int f2(char* x, int f) {
int i=0;
free(x);
if(f) {
printf("%s", x);
return 1;
}
return 0;
}
int main(int argc, char argv) {
char* x = malloc(10);
return f2(x, argc);
}
Is passing freed pointer to printf considered use-after free?
You'll get undefined behaviour (google that term) several times:
in
f2
you are dereferencingx
once it has been freed, becauseprintf
ingx
with the%s
format specifier will dereferencex
, or in other words it will access the memory pointed byx
, and that memory will have undetermined content after callingfree
.even if you remove the
free(x
), you still get undefined behaviour, because then you areprintf
ingx
, whilex
is pointing to valid but non initialized memory.