本文介绍了realloc零字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! C标准没有说明当你用尺寸参数为0调用 realloc时会发生什么。glibc和openbsd似乎都是 返回一个指向零大小对象的有效指针。例如返回一个 malloc(0)。 有没有人知道运行时realloc()free''ed​​对象和 然后返回NULL?如果是这样,它将使以下成语为 realloc()可利用。这是成语,来自openbsd手册页: if((p2 = realloc(p,nsize))== NULL){ 如果(p) free(p); p = NULL; 返回NULL; } p = p2; 你可以看到,如果nsize是0并且realloc()free''的内存和 返回NULL,它将是一个双倍的p。 谢谢, rCs 解决方案 标准(C99)说: 7.20.3.4-3 " ...如果无法分配新对象的内存,则旧对象 未取消分配,其值不变。 另外: 7.20.3.4-4 " realloc函数返回一个指向新对象的指针(可能是具有与指向旧对象的指针相同的值,或者如果无法分配新对象,则为空指针 。 如果realloc失败,则p保留其旧值。如果当你将传递给realloc时p为NULL,你就已经知道它是NULL并且你不想免费 它。 p = NULL; 返回NULL; } p = p2; 你可以看到,如果nsize是0并且realloc()free''内存并且 返回NULL,那么它将是p的两倍。 Realloc不会释放内存并返回NULL,因为标准说 它没有。 - Ioan - Ciprian Tandau tandau _at_ freeshell _dot_ org(希望现在还不算太晚) (。 ..它仍然有效......) 标准(C99)说: 7.20 .3.4-3 " ...如果无法分配新对象的内存,则旧对象将被取消分配,并且其值不变。 另外: 7.20.3.4-4 " realloc函数返回一个指向新对象的指针(可能是 与指向旧对象的指针具有相同的值,或者如果无法分配新对象,则为空指针 。 但是如果大小为0,那么分配内存就不会有任何问题,所以 将不会分配任何东西,现有内存将被释放。 当然它可以释放内存并返回指向零大小块的指针。 - Ian Collins。 标准(C99)说: 7.20.3.4-3 ...如果无法分配新对象的内存,则不会取消分配旧对象,并且其值不变。另外: 7.20.3.4-4 " realloc函数返回一个指向新对象的指针(可能与指向旧对象的指针具有相同的值),或者如果新对象不能指定则返回空指针已分配。 但如果大小为0,那么分配内存就不会有任何问题,所以 什么都不会已分配并释放现有内存。 它将返回一个不为NULL且p2 == NULL的有效指针 将为false。 当然它可以释放内存并返回指向零大小块的指针。 有效指针'不是NULL。 - Ioan - Ciprian Tandau tandau _at_ freeshell _dot_ org(希望它还不算太晚) (......它仍然有效......)The C standard doesn''t say anything about what happens when you callrealloc with a size argument of 0. Both glibc and openbsd appear toreturn a valid pointer to a zero-sized object.. e.g. the return of amalloc(0).Does anyone know of a runtime where realloc() free''ed the object andthen returned NULL? If so, it would make the following idiom forrealloc() exploitable. Here''s the idiom, snagged from an openbsd man page:if ((p2 = realloc(p, nsize)) == NULL) {if (p)free(p);p = NULL;return NULL;}p = p2;You can see that if nsize is 0 and realloc() free''ed the memory andreturned NULL, it would be a double-free of p.Thanks,rCs 解决方案The standard (C99) says:7.20.3.4-3"... If memory for the new object cannot be allocated, the old object isnot deallocated and its value is unchanged.".Also:7.20.3.4-4"The realloc function returns a pointer to the new object (which mayhave the same value as a pointer to the old object), or a null pointerif the new object could not be allocated."If realloc fails then p retains its old value. If p is NULL when youpass it to realloc you already know it''s NULL and you don''t want to freeit.Realloc won''t free the memory and return NULL because the standard saysit doesn''t.--Ioan - Ciprian Tandautandau _at_ freeshell _dot_ org (hope it''s not too late)(... and that it still works...)The standard (C99) says:7.20.3.4-3"... If memory for the new object cannot be allocated, the old object isnot deallocated and its value is unchanged.".Also:7.20.3.4-4"The realloc function returns a pointer to the new object (which mayhave the same value as a pointer to the old object), or a null pointerif the new object could not be allocated."But if size is 0, there won''t be any problems allocating he memory, sonothing will be allocated and the existing memory will be freed.Surely it may free the memory and return a pointer to a zero sized block.--Ian Collins. The standard (C99) says:7.20.3.4-3"... If memory for the new object cannot be allocated, the old object isnot deallocated and its value is unchanged.".Also:7.20.3.4-4"The realloc function returns a pointer to the new object (which mayhave the same value as a pointer to the old object), or a null pointerif the new object could not be allocated."But if size is 0, there won''t be any problems allocating he memory, sonothing will be allocated and the existing memory will be freed.And it will return a valid pointer that will not be NULL and p2==NULLwill be false.Surely it may free the memory and return a pointer to a zero sized block.A valid pointer that''s not NULL.--Ioan - Ciprian Tandautandau _at_ freeshell _dot_ org (hope it''s not too late)(... and that it still works...) 这篇关于realloc零字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 10:59