Skip to content

Commit 809048a

Browse files
committed
Ensure the result of snprintf will not be truncated
Addressing: /home/jcerny/openscap/src/OVAL/probes/unix/linux/inetlisteningservers_probe.c: In function ‘collect_process_info.constprop’: /home/jcerny/openscap/src/OVAL/probes/unix/linux/inetlisteningservers_probe.c:259:26: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size between 156 and 255 [-Wformat-t runcation=] snprintf(ln, 256, "%s/%s", buf, ent->d_name); ^~ /home/jcerny/openscap/src/OVAL/probes/unix/linux/inetlisteningservers_probe.c:259:4: note: ‘snprintf’ output between 2 and 356 bytes into a destination of size 256 snprintf(ln, 256, "%s/%s", buf, ent->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent 711a5c4 commit 809048a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/OVAL/probes/unix/linux/inetlisteningservers_probe.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,25 @@ static int collect_process_info(llist *l)
249249
}
250250
// For each file in the fd dir...
251251
while (( ent = readdir(f) )) {
252-
char line[256], ln[256], *s, *e;
252+
char line[256], *s, *e;
253253
unsigned long inode;
254254
lnode node;
255255
int lnlen;
256256

257257
if (ent->d_name[0] == '.')
258258
continue;
259-
snprintf(ln, 256, "%s/%s", buf, ent->d_name);
260-
if ((lnlen = readlink(ln, line, sizeof(line)-1)) < 0)
259+
int pathname_len = snprintf(NULL, 0, "%s/%s", buf, ent->d_name);
260+
if (pathname_len < 0) {
261261
continue;
262+
}
263+
pathname_len++; // +1 for terminating '\0'
264+
char *pathname = malloc(pathname_len);
265+
snprintf(pathname, pathname_len, "%s/%s", buf, ent->d_name);
266+
if ((lnlen = readlink(pathname, line, sizeof(line) - 1)) < 0) {
267+
free(pathname);
268+
continue;
269+
}
270+
free(pathname);
262271
line[lnlen] = 0;
263272

264273
// Only look at the socket entries

0 commit comments

Comments
 (0)