搓是什么意思| 的字五行属什么| 沙僧是什么动物| 化疗后骨髓抑制是什么意思| 388是什么意思| 苍白的什么| gh发什么音| 黛力新主治什么病| 23数字代表什么意思| 彘是什么意思| 鳝鱼吃什么食物| 荆棘什么意思| bpm是什么| 非洲说什么语言| 三体是什么| 乙巳年是什么命| 立春是什么生肖| 爸爸过生日送什么礼物| 5羟色胺是什么| 弥漫什么意思| 人为什么会梦游| 双子座是什么星象| 什么是偏印| 孱弱是什么意思| 梦见小青蛇是什么预兆| 菠萝蜜过敏什么症状| 兔子的耳朵像什么| 蓝莓和什么不能一起吃| 脚二拇指比大拇指长代表什么| 补铁吃什么| 巴基斯坦是什么人种| 省委组织部长是什么级别| 蚜虫长什么样| 老是干咳嗽是什么原因| 凶宅是什么意思| 水猴子长什么样| 牛肉和什么不能一起吃| 吃什么增肥最快| 骨质疏松是什么意思| 一什么新闻| 鸟字旁的字大多和什么有关| 什么是甲减有什么症状| 感谢老师送什么花| 口疮吃什么药| 出院记录是什么| 什么的树丛| 礼仪是什么| 牙冠是什么意思| 麦芽糖醇是什么| 落枕是什么意思| 奥美拉唑治什么胃病| 男人有美人尖代表什么| 动漫是什么意思| 左手经常发麻是什么原因引起的| 飞龙在天是什么生肖| 补钙吃什么食物| 粉色药片是什么药| 规格是什么意思| 乙肝病毒表面抗原阳性是什么意思| 怀孕吃辣对胎儿有什么影响| 特警力量第二部叫什么| 什么的白塔| 屈打成招是什么意思| 今天有什么新闻| 经常晕车是什么原因| 料酒和黄酒有什么区别| 老年人生日送什么礼物| 11.18是什么星座| 咖啡不能和什么一起吃| 有什么办法| 吃什么补性功能最快| 什么都不怕| 车机是什么| 念珠菌吃什么药最好| 连连支付是什么| 艾滋病的症状是什么样| 下肢静脉曲张是什么原因引起的| 有什么神话故事| 苏慧伦为什么不老| 什么车子寸步难行脑筋急转弯| 不典型鳞状细胞是什么意思| 水煮鱼用什么鱼| 属虎的和什么属相最配| 吃什么会自然流产| 手发胀是什么原因| 7.13什么星座| 吃伟哥有什么副作用| 月经吃什么食物好| 支气管炎什么症状| 预防脑梗用什么药效果好| 补铁的药什么时候吃最好| 奶粉罐可以做什么手工| 眼睛痒是什么原因引起的| 标新立异什么意思| 打喷嚏流鼻涕属于什么感冒| 牙龈肿痛吃什么药效果好| 奶粉中的opo是什么| 国粹是什么| 2点是什么时辰| 中国第五大发明是什么| 数字17代表什么意思| 手脱皮是缺什么维生素| 贫血是什么原因| 带资进组什么意思| 什么是植物| 什么拂面| 缩阳是什么意思| 去取环前需做什么准备| 94年属什么| 鸭肫是什么部位| 蚝油是用什么做的| 肩膀的肌肉叫什么| 屁特别多是什么原因| 炜字五行属什么| 喝胶原蛋白肽有什么好处| 牙周炎吃什么药最好| 钙片什么时间吃最好| 尼古拉斯是什么意思| 石斛配什么泡水喝好| 大豆指的是什么豆| 十月十日什么星座| studio什么牌子| 什么是职务| 血塞通治什么病最好| 人为什么要日b| 野蛮生长是什么意思| 乏力是什么感觉| 女人阳虚吃什么药效果最好| 色弱什么意思| 心梗是什么病| 广东有什么市| 晚上总是睡不着觉是什么原因| dsa检查是什么| 金陵十二钗是什么意思| aimee是什么意思| 什么树叶| 北京晚上有什么好玩的景点| 腰痛吃什么药| 大肠杆菌感染吃什么药| 什么寒什么暖| 寿终正寝是什么意思| 榴莲皮有什么功效| 铁蛋白高吃什么药能降下来| 恶心干呕吃什么药| 什么是公因数| 李时珍的皮是什么意思| 为什么一喝酒就头疼| 浑身出汗是什么原因| 白色裤子配什么上衣好看| 盘古是一个什么样的人| 尤加一笔是什么字| 小孩舌头白是什么原因| 青金石五行属什么| 人类的祖先是什么动物| 特首是什么意思| 中老年人补钙吃什么牌子的钙片好| 老虎下山下一句是什么| 五月是什么星座的啊| nmr是什么意思| 墙内开花墙外香是什么意思| 尿蛋白质阳性是什么意思| 建日是什么意思| 什么是心脏造影| 乳腺增生样改变是什么意思| 郡主是什么身份| 管状腺瘤是什么意思| 流鼻血看病挂什么科| 脐带血能治疗什么病| 5个月宝宝吃什么辅食| 希五行属什么| 什锦是什么水果| 蛇字五行属什么| 电解质水是什么水| 国务院秘书长什么级别| 燕窝是补什么的| 天蝎座有什么特点| 车震是什么意思| 南昌有什么好玩的| 哮喘是什么症状| 中国女人裹脚是从什么时候开始| 星光是什么意思| 脑补是什么意思| 羊水穿刺是查什么的| 三月八号什么星座| 闭角型青光眼是什么意思| 不完全性右束支传导阻滞是什么意思| 临床治愈什么意思| 摘胆对身体有什么影响| 舌边有齿痕是什么原因| 亲嘴为什么要伸舌头| pt什么意思| 小腿肚酸疼是什么原因| 右腿麻木是什么原因| bmi什么意思| t什么意思| 胸一大一小什么原因| 后背中心疼是什么原因| 梦见自己爷爷死了是什么预兆| 女性腋臭什么年龄消失| 胆囊壁厚是什么意思| 皮肤晒伤用什么药| 荷尔蒙爆发是什么意思| 什么叫抗氧化| 97年是什么年| 缺钙吃什么补得最快| 医院打耳洞挂什么科| 崇洋媚外是什么意思| 2月7日是什么星座| 10月份是什么星座的| 什么人适合吃西洋参| 一夫一妻制产生于什么时期| 缘分是什么意思| 能级是什么意思| 天庭饱满是什么意思| 射精无力是什么原因| 四肢无力是什么病| 仔仔是什么意思| 梅花什么季节开| 晚上8点到9点是什么时辰| 什么什么多彩| ecpm是什么意思| aep是什么意思| 神经性皮炎用什么药好| 一闪一闪的星星像什么| 降压药什么时候吃| 2月5号什么星座| 伏是什么意思| 什么病不能吃松花粉| 蚕丝衣服用什么洗最好| 德字五行属什么| 女人梦见烧纸什么预兆| 有因必有果什么意思| 梦到捉鱼是什么意思| 血色素是什么| 乳腺4a类是什么意思| 左胸下方是什么部位| 东施效颦的意思是什么| 月经2个月没来是什么原因| 大麦和小麦有什么区别| 妖魔鬼怪是什么生肖| 包涵是什么意思| 茯砖茶是什么茶| 什么动物有四个胃| 喉咙发炎吃什么消炎药| 一语惊醒梦中人是什么意思| 黄芪配什么不上火| 麦昆牌子是什么档次| 胸闷气短吃什么特效药| 今天过生日是什么星座| 剑突下是什么位置| 查黄体酮做什么检查| 三岁宝宝喝什么奶粉比较好| 梦见修路是什么预兆| 新生儿甲状腺偏高有什么影响| 五行缺什么查询免费| 中国是什么人种| 叫什么| 11.19是什么星座| 经常梦遗是什么原因| 震撼是什么意思| 什么叫蛇胆疮| 保温杯什么牌子好| 什么的梦想| 怹是什么意思| 百度Jump to content

From Wikipedia, the free encyclopedia
Dangling pointer
百度 而随着监管强力纠正同业业务中的不规范之处,曾经风光无限的同业理财迅速由盛转衰,去年同业理财规模较年初大减万亿元,降幅高达五成以上。

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations. More generally, dangling references and wild references are references that do not resolve to a valid destination.

Dangling pointers arise during object destruction, when an object that is pointed to by a given pointer is deleted or deallocated, without modifying the value of that said pointer, so that the pointer still points to the memory location of the deallocated memory. The system may reallocate the previously freed memory, and if the program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. If the program writes to memory referenced by a dangling pointer, a silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find. If the memory has been reallocated to another process, then attempting to dereference the dangling pointer can cause segmentation faults (UNIX, Linux) or general protection faults (Windows). If the program has sufficient privileges to allow it to overwrite the bookkeeping data used by the kernel's memory allocator, the corruption can cause system instabilities. In object-oriented languages with garbage collection, dangling references are prevented by only destroying objects that are unreachable, meaning they do not have any incoming pointers; this is ensured either by tracing or reference counting. However, a finalizer may create new references to an object, requiring object resurrection to prevent a dangling reference.

Wild pointers, also called uninitialized pointers, arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same erratic behavior as dangling pointers, though they are less likely to stay undetected because many compilers will raise a warning at compile time if declared variables are accessed before being initialized.[1]

Cause of dangling pointers

[edit]

In many languages (e.g., the C programming language) deleting an object from memory explicitly or by destroying the stack frame on return does not alter associated pointers. The pointer still points to the same location in memory even though that location may now be used for other purposes.

A straightforward example is shown below:

{
   char *dp = NULL;
   /* ... */
   {
       char c;
       dp = &c;
   } 
     /* c falls out of scope */
     /* dp is now a dangling pointer */
}

If the operating system is able to detect run-time references to null pointers, a solution to the above is to assign 0 (null) to dp immediately before the inner block is exited. Another solution would be to somehow guarantee dp is not used again without further initialization.

Another frequent source of dangling pointers is a jumbled combination of malloc() and free() library calls: a pointer becomes dangling when the block of memory it points to is freed. As with the previous example one way to avoid this is to make sure to reset the pointer to null after freeing its reference—as demonstrated below.

#include <stdlib.h>

void func()
{
    char *dp = malloc(A_CONST);
    /* ... */
    free(dp);         /* dp now becomes a dangling pointer */
    dp = NULL;        /* dp is no longer dangling */
    /* ... */
}

An all too common misstep is returning addresses of a stack-allocated local variable: once a called function returns, the space for these variables gets deallocated and technically they have "garbage values".

int *func(void)
{
    int num = 1234;
    /* ... */
    return &num;
}

Attempts to read from the pointer may still return the correct value (1234) for a while after calling func, but any functions called thereafter may overwrite the stack storage allocated for num with other values and the pointer would no longer work correctly. If a pointer to num must be returned, num must have scope beyond the function—it might be declared as static.

Manual deallocation without dangling reference

[edit]

Antoni Kreczmar [pl] (1945–1996) has created a complete object management system which is free of dangling reference phenomenon.[2] A similar approach was proposed by Fisher and LeBlanc[3] under the name Locks-and-keys.

Cause of wild pointers

[edit]

Wild pointers are created by omitting necessary initialization prior to first use. Thus, strictly speaking, every pointer in programming languages which do not enforce initialization begins as a wild pointer.

This most often occurs due to jumping over the initialization, not by omitting it. Most compilers are able to warn about this.

int f(int i)
{
    char *dp;    /* dp is a wild pointer */
    static char *scp;  /* scp is not a wild pointer:
                        * static variables are initialized to 0
                        * at start and retain their values from
                        * the last call afterwards.
                        * Using this feature may be considered bad
                        * style if not commented */
}

Security holes involving dangling pointers

[edit]

Like buffer-overflow bugs, dangling/wild pointer bugs frequently become security holes. For example, if the pointer is used to make a virtual function call, a different address (possibly pointing at exploit code) may be called due to the vtable pointer being overwritten. Alternatively, if the pointer is used for writing to memory, some other data structure may be corrupted. Even if the memory is only read once the pointer becomes dangling, it can lead to information leaks (if interesting data is put in the next structure allocated there) or to privilege escalation (if the now-invalid memory is used in security checks). When a dangling pointer is used after it has been freed without allocating a new chunk of memory to it, this becomes known as a "use after free" vulnerability.[4] For example, CVE-2014-1776 is a use-after-free vulnerability in Microsoft Internet Explorer 6 through 11[5] being used by zero-day attacks by an advanced persistent threat.[6]

Avoiding dangling pointer errors

[edit]

In C, the simplest technique is to implement an alternative version of the free() (or alike) function which guarantees the reset of the pointer. However, this technique will not clear other pointer variables which may contain a copy of the pointer.

#include <assert.h>
#include <stdlib.h>

/* Alternative version for 'free()' */
static void safefree(void **pp)
{
    /* in debug mode, abort if pp is NULL */
    assert(pp);
    /* free(NULL) works properly, so no check is required besides the assert in debug mode */
    free(*pp);                  /* deallocate chunk, note that free(NULL) is valid */
    *pp = NULL;                 /* reset original pointer */
}

int f(int i)
{
    char *p = NULL, *p2;
    p = malloc(1000);    /* get a chunk */
    p2 = p;              /* copy the pointer */
    /* use the chunk here */
    safefree((void **)&p);       /* safety freeing; does not affect p2 variable */
    safefree((void **)&p);       /* this second call won't fail as p is reset to NULL */
    char c = *p2;       /* p2 is still a dangling pointer, so this is undefined behavior. */
    return i + c;
}

The alternative version can be used even to guarantee the validity of an empty pointer before calling malloc():

safefree(&p);        /* I'm not sure if chunk has been released */
p = malloc(1000);    /* allocate now */

These uses can be masked through #define directives to construct useful macros (a common one being #define XFREE(ptr) safefree((void **)&(ptr))), creating something like a metalanguage or can be embedded into a tool library apart. In every case, programmers using this technique should use the safe versions in every instance where free() would be used; failing in doing so leads again to the problem. Also, this solution is limited to the scope of a single program or project, and should be properly documented.

Among more structured solutions, a popular technique to avoid dangling pointers in C++ is to use smart pointers. A smart pointer typically uses reference counting to reclaim objects. Some other techniques include the tombstones method and the locks-and-keys method.[3]

Another approach is to use the Boehm garbage collector, a conservative garbage collector that replaces standard memory allocation functions in C and C++ with a garbage collector. This approach completely eliminates dangling pointer errors by disabling frees, and reclaiming objects by garbage collection.

Another approach is to use a system such as CHERI, which stores pointers with additional metadata which may prevent invalid accesses by including lifetime information in pointers. CHERI typically requires support in the CPU to conduct these additional checks.

In languages like Java, dangling pointers cannot occur because there is no mechanism to explicitly deallocate memory. Rather, the garbage collector may deallocate memory, but only when the object is no longer reachable from any references.

In the language Rust, the type system has been extended to include also the variables lifetimes and resource acquisition is initialization. Unless one disables the features of the language, dangling pointers will be caught at compile time and reported as programming errors.

Dangling pointer detection

[edit]

To expose dangling pointer errors, one common programming technique is to set pointers to the null pointer or to an invalid address once the storage they point to has been released. When the null pointer is dereferenced (in most languages) the program will immediately terminate—there is no potential for data corruption or unpredictable behavior. This makes the underlying programming mistake easier to find and resolve. This technique does not help when there are multiple copies of the pointer.

Some debuggers will automatically overwrite and destroy data that has been freed, usually with a specific pattern, such as 0xDEADBEEF (Microsoft's Visual C/C++ debugger, for example, uses 0xCC, 0xCD or 0xDD depending on what has been freed[7]). This usually prevents the data from being reused by making it useless and also very prominent (the pattern serves to show the programmer that the memory has already been freed).

Tools such as Polyspace, TotalView, Valgrind, Mudflap,[8] AddressSanitizer, or tools based on LLVM[9] can also be used to detect uses of dangling pointers.

Other tools (SoftBound, Insure++, and CheckPointer) instrument the source code to collect and track legitimate values for pointers ("metadata") and check each pointer access against the metadata for validity.

Another strategy, when suspecting a small set of classes, is to temporarily make all their member functions virtual: after the class instance has been destructed/freed, its pointer to the Virtual Method Table is set to NULL, and any call to a member function will crash the program and it will show the guilty code in the debugger.

The ARM64 memory tagging extension (MTE) - disabled by default on Linux systems, but can be enabled on Android 16 - triggers a segmentation fault when it detects use-after-free and buffer overflow.[10][11]

See also

[edit]

References

[edit]
  1. ^ "Warning Options - Using the GNU Compiler Collection (GCC)".
  2. ^ Gianna Cioni, Antoni Kreczmar, Programmed deallocation without dangling reference, Information Processing Letters, v. 18, 1984, pp. 179–185
  3. ^ a b C. N. Fisher, R. J. Leblanc, The implementation of run-time diagnostics in Pascal , IEEE Transactions on Software Engineering, 6(4):313–319, 1980.
  4. ^ Dalci, Eric; anonymous author; CWE Content Team (May 11, 2012). "CWE-416: Use After Free". Common Weakness Enumeration. Mitre Corporation. Retrieved April 28, 2014. {{cite web}}: |author2= has generic name (help)
  5. ^ "CVE-2014-1776". Common Vulnerabilities and Exposures (CVE). 2025-08-05. Archived from the original on 2025-08-05. Retrieved 2025-08-05.
  6. ^ Chen, Xiaobo; Caselden, Dan; Scott, Mike (April 26, 2014). "New Zero-Day Exploit targeting Internet Explorer Versions 9 through 11 Identified in Targeted Attacks". FireEye Blog. FireEye. Retrieved April 28, 2014.[permanent dead link]
  7. ^ Visual C++ 6.0 memory-fill patterns
  8. ^ Mudflap Pointer Debugging
  9. ^ Dhurjati, D. and Adve, V. Efficiently Detecting All Dangling Pointer Uses in Production Servers
  10. ^ "Arm memory tagging extension". Android Open Source Project. Retrieved 2025-08-05.
  11. ^ Goodin, Dan (2025-08-05). "Google introduces Advanced Protection mode for its most at-risk Android users". Ars Technica. Retrieved 2025-08-05.
完蛋是什么意思 右眼跳是什么兆头 一什么玉米 一语道破什么意思 同房有点痛什么原因
一幅什么 胆汁淤积症有什么症状 小分子肽能治什么病 梦见火车脱轨什么预兆 什么的娃娃
粉刺是什么样的图片 脚背肿是什么原因 流口水是什么病 地中海贫血是什么原因引起的 progress什么意思
野蛮生长是什么意思 1997年7月1日属什么生肖 甲状腺密度不均匀是什么意思 孩子为什么说话迟 健脾祛湿吃什么药效果最好
快照是什么意思hcv9jop7ns9r.cn 手发抖是什么原因引起的qingzhougame.com 什么是有机奶粉hcv8jop6ns5r.cn 人是什么结构jingluanji.com 晚上吃什么减肥快hcv7jop6ns3r.cn
早上起床手指肿胀是什么原因hcv7jop9ns8r.cn 魁元是什么意思zhongyiyatai.com 男人前列腺在什么位置hcv7jop9ns9r.cn 78年属什么hcv9jop0ns2r.cn 髂胫束在什么位置hcv7jop4ns8r.cn
花甲之年是什么意思hcv8jop9ns5r.cn 太极是什么hcv8jop9ns3r.cn 血浆是什么颜色hcv9jop2ns5r.cn 毛囊炎的症状是什么原因引起的onlinewuye.com 舌息心念什么hcv8jop5ns2r.cn
做爱是什么感觉hcv7jop5ns1r.cn 女生打呼噜是什么原因qingzhougame.com 刮宫是什么意思hcv9jop1ns1r.cn 85年属什么生肖hcv9jop3ns9r.cn 金多水浊什么意思hcv7jop7ns4r.cn
百度