Skip to content

Commit d1d5c07

Browse files
cgzonesBenBE
authored andcommitted
Silence clang analyzer warnings
linux/LinuxProcessTable.c:795:11: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream] 795 | while (fgets(buffer, sizeof(buffer), fp)) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxProcessTable.c:795:11: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream] 795 | while (fgets(buffer, sizeof(buffer), fp)) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxProcessTable.c:840:11: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream] 840 | while (fgets(linebuf, sizeof(linebuf), file) != NULL) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxProcessTable.c:840:11: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream] 840 | while (fgets(linebuf, sizeof(linebuf), file) != NULL) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxMachine.c:81:22: warning: The 1st argument to 'openat' is between -99 and -1 but should be a valid file descriptor or AT_FDCWD [unix.StdCLibraryFunctions] 81 | int cpuDirFd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxMachine.c:501:11: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream] 501 | while (fgets(buffer, sizeof(buffer), file)) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/LinuxMachine.c:501:11: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream] 501 | while (fgets(buffer, sizeof(buffer), file)) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/Platform.c:467:22: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream] 467 | } while ((bytes = fread(env + size, 1, capacity - size, fp)) > 0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/Platform.c:467:22: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream] 467 | } while ((bytes = fread(env + size, 1, capacity - size, fp)) > 0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/Platform.c:585:15: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream] 585 | total = fscanf(fp, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/Platform.c:585:15: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream] 585 | total = fscanf(fp, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linux/Platform.c:789:21: warning: The 1st argument to 'openat' is between -99 and -1 but should be a valid file descriptor or AT_FDCWD [unix.StdCLibraryFunctions] 789 | int entryFd = openat(dirfd(dir), entryName, O_DIRECTORY | O_PATH); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent f9bb289 commit d1d5c07

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

XUtils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ in the source distribution for its full text.
1414
#error "Must have #include \"config.h\" line at the top of the file that includes these XUtils helper functions"
1515
#endif
1616

17+
#include <dirent.h>
1718
#include <stdbool.h>
1819
#include <stddef.h> // IWYU pragma: keep
1920
#include <stdio.h>
@@ -152,4 +153,20 @@ unsigned int countTrailingZeros(unsigned int x);
152153
/* IEC unit prefixes */
153154
static const char unitPrefixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };
154155

156+
static inline bool skipEndOfLine(FILE* fp) {
157+
char buffer[1024];
158+
while (fgets(buffer, sizeof(buffer), fp)) {
159+
if (strchr(buffer, '\n')) {
160+
return true;
161+
}
162+
}
163+
return false;
164+
}
165+
166+
static inline int xDirfd(DIR* dirp) {
167+
int r = dirfd(dirp);
168+
assert(r >= 0);
169+
return r;
170+
}
171+
155172
#endif

linux/LinuxMachine.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void LinuxMachine_updateCPUcount(LinuxMachine* this) {
7878
continue;
7979

8080
#ifdef HAVE_OPENAT
81-
int cpuDirFd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW);
81+
int cpuDirFd = openat(xDirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW);
8282
if (cpuDirFd < 0)
8383
continue;
8484
#else
@@ -497,11 +497,13 @@ static void LinuxMachine_scanCPUTime(LinuxMachine* this) {
497497

498498
this->period = (double)this->cpuData[0].totalPeriod / super->activeCPUs;
499499

500-
char buffer[PROC_LINE_LENGTH + 1];
501-
while (fgets(buffer, sizeof(buffer), file)) {
502-
if (String_startsWith(buffer, "procs_running")) {
503-
this->runningTasks = (unsigned int) strtoul(buffer + strlen("procs_running"), NULL, 10);
504-
break;
500+
if (!ferror(file) && !feof(file)) {
501+
char buffer[PROC_LINE_LENGTH + 1];
502+
while (fgets(buffer, sizeof(buffer), file)) {
503+
if (String_startsWith(buffer, "procs_running")) {
504+
this->runningTasks = (unsigned int) strtoul(buffer + strlen("procs_running"), NULL, 10);
505+
break;
506+
}
505507
}
506508
}
507509

linux/LinuxProcessTable.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,11 @@ static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t
795795
while (fgets(buffer, sizeof(buffer), fp)) {
796796
if (!strchr(buffer, '\n')) {
797797
// Partial line, skip to end of this line
798-
while (fgets(buffer, sizeof(buffer), fp)) {
799-
if (strchr(buffer, '\n')) {
800-
break;
801-
}
798+
if (!skipEndOfLine(fp)) {
799+
fclose(fp);
800+
return false;
802801
}
803-
continue;
802+
804803
}
805804

806805
if (String_startsWith(buffer, "Pss:")) {
@@ -840,12 +839,9 @@ static void LinuxProcessTable_readOpenVZData(LinuxProcess* process, openat_arg_t
840839
while (fgets(linebuf, sizeof(linebuf), file) != NULL) {
841840
if (strchr(linebuf, '\n') == NULL) {
842841
// Partial line, skip to end of this line
843-
while (fgets(linebuf, sizeof(linebuf), file) != NULL) {
844-
if (strchr(linebuf, '\n') != NULL) {
845-
break;
846-
}
842+
if (!skipEndOfLine(file)) {
843+
break;
847844
}
848-
continue;
849845
}
850846

851847
char* name_value_sep = strchr(linebuf, ':');

linux/Platform.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ char* Platform_getProcessEnv(pid_t pid) {
464464
size += bytes;
465465
capacity += 4096;
466466
env = xRealloc(env, capacity);
467-
} while ((bytes = fread(env + size, 1, capacity - size, fp)) > 0);
467+
} while (!ferror(fp) && !feof(fp) && (bytes = fread(env + size, 1, capacity - size, fp)) > 0);
468468

469469
fclose(fp);
470470

@@ -581,7 +581,7 @@ void Platform_getPressureStall(const char* file, bool some, double* ten, double*
581581
return;
582582
}
583583
int total = fscanf(fp, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
584-
if (!some) {
584+
if (total != EOF && !some) {
585585
total = fscanf(fp, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
586586
}
587587
(void) total;
@@ -786,7 +786,7 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
786786
const char* entryName = dirEntry->d_name;
787787

788788
#ifdef HAVE_OPENAT
789-
int entryFd = openat(dirfd(dir), entryName, O_DIRECTORY | O_PATH);
789+
int entryFd = openat(xDirfd(dir), entryName, O_DIRECTORY | O_PATH);
790790
if (entryFd < 0)
791791
continue;
792792
#else

0 commit comments

Comments
 (0)