summaryrefslogtreecommitdiffstats
path: root/linux
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2024-01-20 11:51:20 +0100
committerBenBE <BenBE@geshi.org>2024-01-25 10:03:26 +0100
commit94c78224015bfb461a124b994a78c37f8c2a3e34 (patch)
tree56bec236e69248302b79a9548cf1aa4aaf323775 /linux
parent207db2e8f8874de7b8cfe43fa5ce7cb7c06b5097 (diff)
Explicitly check sscanf(3) and fscanf(3) return values
Compare the return value of sscanf(3) and fscanf(3) explicitly against the expected number of parsed items and avoid implicit boolean conversion. Such an implicit conversion would treat EOF (-1) the same as at least one item parsed successfully. Reported by CodeQL.
Diffstat (limited to 'linux')
-rw-r--r--linux/LinuxMachine.c12
-rw-r--r--linux/LinuxProcessTable.c10
-rw-r--r--linux/Platform.c2
3 files changed, 12 insertions, 12 deletions
diff --git a/linux/LinuxMachine.c b/linux/LinuxMachine.c
index 50d181e1..ff2b605a 100644
--- a/linux/LinuxMachine.c
+++ b/linux/LinuxMachine.c
@@ -302,8 +302,8 @@ static void LinuxMachine_scanZramInfo(LinuxMachine* this) {
memory_t orig_data_size = 0;
memory_t compr_data_size = 0;
- if (!fscanf(disksize_file, "%llu\n", &size) ||
- !fscanf(mm_stat_file, " %llu %llu", &orig_data_size, &compr_data_size)) {
+ if (1 != fscanf(disksize_file, "%llu\n", &size) ||
+ 2 != fscanf(mm_stat_file, " %llu %llu", &orig_data_size, &compr_data_size)) {
fclose(disksize_file);
fclose(mm_stat_file);
break;
@@ -342,10 +342,10 @@ static void LinuxMachine_scanZfsArcstats(LinuxMachine* this) {
sscanf(buffer + strlen(label), " %*2u %32llu", variable); \
break; \
} else (void) 0 /* Require a ";" after the macro use. */
- #define tryReadFlag(label, variable, flag) \
- if (String_startsWith(buffer, label)) { \
- (flag) = sscanf(buffer + strlen(label), " %*2u %32llu", variable); \
- break; \
+ #define tryReadFlag(label, variable, flag) \
+ if (String_startsWith(buffer, label)) { \
+ (flag) = (1 == sscanf(buffer + strlen(label), " %*2u %32llu", variable)); \
+ break; \
} else (void) 0 /* Require a ";" after the macro use. */
switch (buffer[0]) {
diff --git a/linux/LinuxProcessTable.c b/linux/LinuxProcessTable.c
index 83b92619..4386f5c0 100644
--- a/linux/LinuxProcessTable.c
+++ b/linux/LinuxProcessTable.c
@@ -426,14 +426,14 @@ static bool LinuxProcessTable_readStatusFile(Process* process, openat_arg_t proc
} else if (String_startsWith(buffer, "voluntary_ctxt_switches:")) {
unsigned long vctxt;
int ok = sscanf(buffer, "voluntary_ctxt_switches:\t%lu", &vctxt);
- if (ok >= 1) {
+ if (ok == 1) {
ctxt += vctxt;
}
} else if (String_startsWith(buffer, "nonvoluntary_ctxt_switches:")) {
unsigned long nvctxt;
int ok = sscanf(buffer, "nonvoluntary_ctxt_switches:\t%lu", &nvctxt);
- if (ok >= 1) {
+ if (ok == 1) {
ctxt += nvctxt;
}
@@ -441,14 +441,14 @@ static bool LinuxProcessTable_readStatusFile(Process* process, openat_arg_t proc
} else if (String_startsWith(buffer, "VxID:")) {
int vxid;
int ok = sscanf(buffer, "VxID:\t%32d", &vxid);
- if (ok >= 1) {
+ if (ok == 1) {
lp->vxid = vxid;
}
#ifdef HAVE_ANCIENT_VSERVER
} else if (String_startsWith(buffer, "s_context:")) {
int vxid;
int ok = sscanf(buffer, "s_context:\t%32d", &vxid);
- if (ok >= 1) {
+ if (ok == 1) {
lp->vxid = vxid;
}
#endif /* HAVE_ANCIENT_VSERVER */
@@ -938,7 +938,7 @@ static void LinuxProcessTable_readOomData(LinuxProcess* process, openat_arg_t pr
if (fgets(buffer, PROC_LINE_LENGTH, file)) {
unsigned int oom;
int ok = sscanf(buffer, "%u", &oom);
- if (ok >= 1) {
+ if (ok == 1) {
process->oom = oom;
}
}
diff --git a/linux/Platform.c b/linux/Platform.c
index 8dc8bb59..af81a694 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -261,7 +261,7 @@ int Platform_getUptime(void) {
if (fd) {
int n = fscanf(fd, "%64lf", &uptime);
fclose(fd);
- if (n <= 0) {
+ if (n != 1) {
return 0;
}
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy