Skip to content

Commit 6b212fb

Browse files
committed
Properly check snprintf return value
1 parent 898c9c9 commit 6b212fb

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

linux/LinuxProcessTable.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ in the source distribution for its full text.
1818
#include <limits.h>
1919
#include <math.h>
2020
#include <stdbool.h>
21+
#include <stddef.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
2324
#include <string.h>
@@ -1040,7 +1041,7 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t
10401041
char output[PROC_LINE_LENGTH + 1];
10411042
output[0] = '\0';
10421043
char* at = output;
1043-
int left = PROC_LINE_LENGTH;
1044+
size_t left = PROC_LINE_LENGTH;
10441045
while (!feof(file) && left > 0) {
10451046
char buffer[PROC_LINE_LENGTH + 1];
10461047
const char* ok = fgets(buffer, PROC_LINE_LENGTH, file);
@@ -1064,8 +1065,14 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t
10641065
at++;
10651066
left--;
10661067
}
1068+
10671069
int wrote = snprintf(at, left, "%s", group);
1068-
left -= wrote;
1070+
if (wrote < 0 || (size_t)wrote >= left) {
1071+
// Output was truncated, we are done
1072+
break;
1073+
}
1074+
1075+
left -= (size_t)wrote;
10691076
}
10701077
fclose(file);
10711078

0 commit comments

Comments
 (0)