What is Nostr?
graywolf /
npub1nef…uq7k
2024-01-28 16:09:21

graywolf on Nostr: Is there anything wrong with this #c code (I am compiling with #gcc 13.2.1)? int ...

Is there anything wrong with this #c code (I am compiling with #gcc 13.2.1)?

int
stravis(char **outp, const char *src, int flag)
{
char *buf;
int len, serrno;

buf = calloc(4, strlen(src) + 1);
if (buf == NULL)
return -1;
len = strvis(buf, src, flag);
serrno = errno;
*outp = realloc(buf, len + 1);
if (*outp == NULL) {
*outp = buf;
errno = serrno;
}
return (len);
}

I am getting #useafterfree warning:

/home/build/repo/compat_vis.c: In function ‘stravis’:
/home/build/repo/compat_vis.c:222:23: warning: pointer ‘buf_15’ may be used after ‘realloc’ [-Wuse-after-free]
222 | *outp = buf;
| ~~~~~~^~~~~
/home/build/repo/compat_vis.c:220:17: note: call to ‘realloc’ here
220 | *outp = realloc(buf, len + 1);
| ^~~~~~~~~~~~~~~~~~~~~

I do not understand why, the code looks correct... What am I missing?
Author Public Key
npub1nefrrwuefssnkj6mc8gjkqfxp0z2sgutw7eclqw3h5ns6qm7g7js58uq7k