This code is writing an extra character at memset, but why?
int main(int argc, char ∗argv[]) {
char ∗a, ∗b;
a=(char ∗)malloc(12);
b=(char ∗)malloc(12);
if(!a || !b)
err(2, "malloc error.\\n");
bzero(a, 12); bzero(b, 12);
printf("%x %x (%i)\n", a, b, b−a); // b−a is d.
memset(a, (int)'a', 11);
memset(b, (int)'b', 11);
printf("a: %s\n", a);
printf("b: %s\n", b);
printf("−−−\n");
memset(a, (int)'a', b−a); // Heap−Overflow? Is this happening here ?
printf("a: %s\n", a);
free(a);
free(b);
return 0;
}
vulnerability? In case this would be a password, there would be an extra character after every print?
”and’are not"and'. What do you think is the result ofb−a?aandbin your example) is undefined behavior.void *) pointer you should use the%pformat specifier. And the result of pointer subtraction is (IIRC) asize_twhich should be printed with%zu. Mismatching format specifiers and argument types leads to undefined behavior.malloc(x)can use more thatxbytes - those extra bytes are for housekeeping.ptrdiff_t, which is not necessarily the same type assize_t. The best way to print one is probably to first cast it to an integer type whose format specifier you know.