On Tue, 1 Sep 2015 12:00:16 +0200, mark <ma...@invalid.invalid> wrote:
>In the paper 'Preventing Use-after-free with Dangling Pointers
>Nullification', there is an example of Chromium using a pointer value
>after delete:
>
>[...]
>delete doc->child;
This implies that doc->child is a pointer (call it T*).
>allChilds[doc->child]=DELETED;
How can a pointer be used as the subscript of an array (or vector)? I
guess if allChilds were of type map<T*, int> and DELETED was an
expression compatible with int, this might make syntactic sense.
>[...]
>
>Is this undefined behavior?
Yes. Any attempt to evaluate a pointer after the memory it points to
has been deallocated is UB. It is not limited to dynamic allocation
either. Consider
int* foo()
{int i = 5;
return &i;}
int main()
{int *p;
int i;
p = foo();
i = *p; // UB-1
cout << p; //also UB-1
}
UB-1: You cannot dereference a pointer when you no longer own the
memory it used to point to.
UB-2: While not that common anymore, some hardware systems still
"validate" an address even if you are not accessing the memory at that
address.
>Per C++11 standard:
>
>3.7.4.2 Deallocation functions
><<<
>If the argument given to a deallocation function in the standard library
>is a pointer that is not the null pointer value (4.10), the deallocation
>function shall deallocate the storage referenced by the pointer,
>rendering invalid all pointers referring to any part of the deallocated
>storage. The effect of using an invalid pointer value (including passing
>it to a deallocation function) is undefined.
> >>>
>
>Does this imply that the example above has UB? This section is unclear
>to me. The "clarification" "including passing it to a deallocation
>function" doesn't help, since it implies a different kind of usage as
>opposed to just using the pointer as numeric value.
I don't know why they added the parenthetical note. It adds nothing
to the sentence. It's like saying "Any attempt to perform arithmetic
(including addition) is ...". Any use of an invalid value is
undefined. Note that the pointer does not become invalid until the
function actually deallocates the storage. So
int *p = new int;
delete p; //OK
delete p; //UB
--
Remove del for email