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?
Published at
2024-01-28 16:09:21Event JSON
{
"id": "99184bbfed6c63587dfa5244078ca1b5af4f4160ad86150c184ef56339b2decf",
"pubkey": "9e5231bb994c213b4b5bc1d12b01260bc4a8238b77b38f81d1bd270d037e47a5",
"created_at": 1706458161,
"kind": 1,
"tags": [
[
"t",
"c"
],
[
"t",
"gcc"
],
[
"t",
"useafterfree"
],
[
"proxy",
"https://emacs.ch/users/graywolf/statuses/111834442061316690",
"activitypub"
]
],
"content": "Is there anything wrong with this #c code (I am compiling with #gcc 13.2.1)?\n\nint\nstravis(char **outp, const char *src, int flag)\n{\n\tchar *buf;\n\tint len, serrno;\n\n\tbuf = calloc(4, strlen(src) + 1);\n\tif (buf == NULL)\n\t\treturn -1;\n\tlen = strvis(buf, src, flag);\n\tserrno = errno;\n\t*outp = realloc(buf, len + 1);\n\tif (*outp == NULL) {\n\t\t*outp = buf;\n\t\terrno = serrno;\n\t}\n\treturn (len);\n}\n\nI am getting #useafterfree warning:\n\n/home/build/repo/compat_vis.c: In function ‘stravis’:\n/home/build/repo/compat_vis.c:222:23: warning: pointer ‘buf_15’ may be used after ‘realloc’ [-Wuse-after-free]\n 222 | *outp = buf;\n | ~~~~~~^~~~~\n/home/build/repo/compat_vis.c:220:17: note: call to ‘realloc’ here\n 220 | *outp = realloc(buf, len + 1);\n | ^~~~~~~~~~~~~~~~~~~~~\n\nI do not understand why, the code looks correct... What am I missing?",
"sig": "7301240bf80c2c874dba33651738ef9968a5e00a02c1e98a46fc433b77989feab8cb6e21709a3395ef50e6fd9cec26c3f73b6f67a4370af57eb7f1f972ed5890"
}