Skip to content

Commit 805fc15

Browse files
committed
Process: Use size_t for "cmdline" and "procExe" offsets
Convert the members "cmdlineBasenameStart", "cmdlineBasenameEnd" and "procExeBasenameOffset" in "Process" to size_t type. Also upgrade many routines that search for "basenames", COMM, and PROC_EXE string to use size_t for iterators. The "cmdlineBasenameEnd" value is no longer allowed to negative. It is now set to 0 during Process_init(). Signed-off-by: Kang-Che Sung <[email protected]>
1 parent a753894 commit 805fc15

File tree

9 files changed

+88
-91
lines changed

9 files changed

+88
-91
lines changed

Process.c

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,25 @@ void Process_fillStarttimeBuffer(Process* this) {
6464
*/
6565
#define TASK_COMM_LEN 16
6666

67-
static bool findCommInCmdline(const char* comm, const char* cmdline, int cmdlineBasenameStart, int* pCommStart, int* pCommLen) {
67+
static bool findCommInCmdline(const char* comm, const char* cmdline, size_t cmdlineBasenameStart, size_t* pCommStart, size_t* pCommLen) {
6868
/* Try to find procComm in tokenized cmdline - this might in rare cases
6969
* mis-identify a string or fail, if comm or cmdline had been unsuitably
7070
* modified by the process */
7171
const char* tokenBase;
7272
size_t tokenLen;
7373
const size_t commLen = strlen(comm);
7474

75-
if (cmdlineBasenameStart < 0)
76-
return false;
77-
7875
for (const char* token = cmdline + cmdlineBasenameStart; *token;) {
7976
for (tokenBase = token; *token && *token != '\n'; ++token) {
8077
if (*token == '/') {
8178
tokenBase = token + 1;
8279
}
8380
}
84-
tokenLen = token - tokenBase;
81+
tokenLen = (size_t)(token - tokenBase);
8582

8683
if ((tokenLen == commLen || (tokenLen > commLen && commLen == (TASK_COMM_LEN - 1))) &&
8784
strncmp(tokenBase, comm, commLen) == 0) {
88-
*pCommStart = tokenBase - cmdline;
85+
*pCommStart = (size_t)(tokenBase - cmdline);
8986
*pCommLen = tokenLen;
9087
return true;
9188
}
@@ -99,10 +96,10 @@ static bool findCommInCmdline(const char* comm, const char* cmdline, int cmdline
9996
return false;
10097
}
10198

102-
static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int* cmdlineBasenameStart, const char* exe, int exeBaseOffset, int exeBaseLen) {
99+
static size_t matchCmdlinePrefixWithExeSuffix(const char* cmdline, size_t* cmdlineBasenameStart, const char* exe, size_t exeBaseOffset, size_t exeBaseLen) {
103100
/* cmdline prefix is an absolute path: it must match whole exe. */
104101
if (cmdline[0] == '/') {
105-
int matchLen = exeBaseLen + exeBaseOffset;
102+
size_t matchLen = exeBaseLen + exeBaseOffset;
106103
if (strncmp(cmdline, exe, matchLen) == 0) {
107104
char delim = cmdline[matchLen];
108105
if (delim == 0 || delim == '\n' || delim == ' ') {
@@ -124,18 +121,18 @@ static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int* cmdlineBase
124121
*
125122
* So if needed, we adjust cmdlineBaseOffset to the previous (if any)
126123
* component of the cmdline relative path, and retry the procedure. */
127-
int cmdlineBaseOffset = *cmdlineBasenameStart;
124+
size_t cmdlineBaseOffset = *cmdlineBasenameStart;
128125
bool delimFound = true; /* if valid basename delimiter found */
129126
do {
130127
/* match basename */
131-
int matchLen = exeBaseLen + cmdlineBaseOffset;
128+
size_t matchLen = exeBaseLen + cmdlineBaseOffset;
132129
if (cmdlineBaseOffset < exeBaseOffset &&
133130
strncmp(cmdline + cmdlineBaseOffset, exe + exeBaseOffset, exeBaseLen) == 0) {
134131
char delim = cmdline[matchLen];
135132
if (delim == 0 || delim == '\n' || delim == ' ') {
136133
/* reverse match the cmdline prefix and exe suffix */
137-
int i = cmdlineBaseOffset;
138-
int j = exeBaseOffset;
134+
size_t i = cmdlineBaseOffset;
135+
size_t j = exeBaseOffset;
139136
while (i >= 1 && j >= 1 && cmdline[i - 1] == exe[j - 1]) {
140137
--i, --j;
141138
}
@@ -151,6 +148,9 @@ static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int* cmdlineBase
151148
/* Try to find the previous potential cmdlineBaseOffset - it would be
152149
* preceded by '/' or nothing, and delimited by ' ' or '\n' */
153150
delimFound = false;
151+
if (cmdlineBaseOffset <= 2) {
152+
return 0;
153+
}
154154
for (cmdlineBaseOffset -= 2; cmdlineBaseOffset > 0; --cmdlineBaseOffset) {
155155
if (delimFound) {
156156
if (cmdline[cmdlineBaseOffset - 1] == '/') {
@@ -311,8 +311,8 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
311311
char* strStart = mc->str;
312312
char* str = strStart;
313313

314-
int cmdlineBasenameStart = this->cmdlineBasenameStart;
315-
int cmdlineBasenameLen = 0;
314+
size_t cmdlineBasenameStart = this->cmdlineBasenameStart;
315+
size_t cmdlineBasenameLen = 0;
316316
if (this->cmdlineBasenameEnd > this->cmdlineBasenameStart)
317317
cmdlineBasenameLen = this->cmdlineBasenameEnd - this->cmdlineBasenameStart;
318318

@@ -322,20 +322,18 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
322322
cmdline = "(zombie)";
323323
}
324324

325-
assert(cmdlineBasenameStart >= 0);
326-
assert(cmdlineBasenameStart <= (int)strlen(cmdline));
325+
assert(cmdlineBasenameStart <= strlen(cmdline));
327326

328-
int exeLen = 0;
329-
int exeBasenameOffset = 0;
330-
int exeBasenameLen = 0;
331-
int matchLen = 0;
327+
size_t exeLen = 0;
328+
size_t exeBasenameOffset = 0;
329+
size_t exeBasenameLen = 0;
330+
size_t matchLen = 0;
332331
if (procExe) {
333332
exeLen = strlen(procExe);
334333
exeBasenameOffset = this->procExeBasenameOffset;
335334
exeBasenameLen = exeLen - exeBasenameOffset;
336335

337-
assert(exeBasenameOffset >= 0);
338-
assert(exeBasenameOffset <= (int)strlen(procExe));
336+
assert(exeBasenameOffset <= strlen(procExe));
339337

340338
if (this->cmdline) {
341339
matchLen = matchCmdlinePrefixWithExeSuffix(this->cmdline, &cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);
@@ -375,7 +373,7 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
375373
return;
376374
}
377375

378-
int commLen = 0;
376+
size_t commLen = 0;
379377

380378
bool haveCommInExe = false;
381379
if (procExe && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
@@ -386,7 +384,7 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
386384
}
387385

388386
bool haveCommInCmdline = false;
389-
int commStart = 0;
387+
size_t commStart = 0;
390388

391389
if (!haveCommInExe && this->cmdline && procComm && searchCommInCmdline && (!Process_isUserlandThread(this) || showThreadNames)) {
392390
haveCommInCmdline = findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commLen);
@@ -470,20 +468,20 @@ void Process_writeCommand(const Process* this, int attr, int baseAttr, RichStrin
470468
const ProcessMergedCommand* mc = &this->mergedCommand;
471469
const char* mergedCommand = mc->str;
472470

473-
int strStart = RichString_size(str);
471+
size_t strStart = RichString_size(str);
474472

475473
const Settings* settings = this->super.host->settings;
476474
const bool highlightBaseName = settings->highlightBaseName;
477475
const bool highlightSeparator = true;
478476
const bool highlightDeleted = settings->highlightDeletedExe;
479477

480478
if (!mergedCommand) {
481-
int len = 0;
479+
size_t len = 0;
482480
const char* cmdline = this->cmdline;
483481

484482
if (highlightBaseName || !settings->showProgramPath) {
485-
int basename = 0;
486-
for (int i = 0; i < this->cmdlineBasenameEnd; i++) {
483+
size_t basename = 0;
484+
for (size_t i = 0; i < this->cmdlineBasenameEnd; i++) {
487485
if (cmdline[i] == '/') {
488486
basename = i + 1;
489487
} else if (cmdline[i] == ':') {
@@ -874,7 +872,7 @@ bool Process_rowMatchesFilter(const Row* super, const Table* table) {
874872
void Process_init(Process* this, const Machine* host) {
875873
Row_init(&this->super, host);
876874

877-
this->cmdlineBasenameEnd = -1;
875+
this->cmdlineBasenameEnd = 0;
878876
this->st_uid = (uid_t)-1;
879877
}
880878

@@ -1026,12 +1024,12 @@ void Process_updateComm(Process* this, const char* comm) {
10261024
this->mergedCommand.lastUpdate = 0;
10271025
}
10281026

1029-
static int skipPotentialPath(const char* cmdline, int end) {
1027+
static size_t skipPotentialPath(const char* cmdline, size_t end) {
10301028
if (cmdline[0] != '/')
10311029
return 0;
10321030

1033-
int slash = 0;
1034-
for (int i = 1; i < end; i++) {
1031+
size_t slash = 0;
1032+
for (size_t i = 1; i < end; i++) {
10351033
if (cmdline[i] == '/' && cmdline[i + 1] != '\0') {
10361034
slash = i + 1;
10371035
continue;
@@ -1047,11 +1045,10 @@ static int skipPotentialPath(const char* cmdline, int end) {
10471045
return slash;
10481046
}
10491047

1050-
void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd) {
1051-
assert(basenameStart >= 0);
1052-
assert((cmdline && basenameStart < (int)strlen(cmdline)) || (!cmdline && basenameStart == 0));
1048+
void Process_updateCmdline(Process* this, const char* cmdline, size_t basenameStart, size_t basenameEnd) {
1049+
assert((cmdline && basenameStart < strlen(cmdline)) || (!cmdline && basenameStart == 0));
10531050
assert((basenameEnd > basenameStart) || (basenameEnd == 0 && basenameStart == 0));
1054-
assert((cmdline && basenameEnd <= (int)strlen(cmdline)) || (!cmdline && basenameEnd == 0));
1051+
assert((cmdline && basenameEnd <= strlen(cmdline)) || (!cmdline && basenameEnd == 0));
10551052

10561053
if (!this->cmdline && !cmdline)
10571054
return;
@@ -1084,7 +1081,7 @@ void Process_updateExe(Process* this, const char* exe) {
10841081
if (exe) {
10851082
this->procExe = xStrdup(exe);
10861083
const char* lastSlash = strrchr(exe, '/');
1087-
this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0;
1084+
this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (size_t)(lastSlash - exe + 1) : 0;
10881085
} else {
10891086
this->procExe = NULL;
10901087
this->procExeBasenameOffset = 0;

Process.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ typedef struct Process_ {
129129
char* cmdline;
130130

131131
/* End Offset in cmdline of the process basename */
132-
int cmdlineBasenameEnd;
132+
size_t cmdlineBasenameEnd;
133133

134134
/* Start Offset in cmdline of the process basename */
135-
int cmdlineBasenameStart;
135+
size_t cmdlineBasenameStart;
136136

137137
/* The process' "command" name */
138138
char* procComm;
@@ -144,7 +144,7 @@ typedef struct Process_ {
144144
char* procCwd;
145145

146146
/* Offset in procExe of the process basename */
147-
int procExeBasenameOffset;
147+
size_t procExeBasenameOffset;
148148

149149
/* Tells if the executable has been replaced in the filesystem since start */
150150
bool procExeDeleted;
@@ -326,7 +326,7 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField
326326
const char* Process_getCommand(const Process* this);
327327

328328
void Process_updateComm(Process* this, const char* comm);
329-
void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd);
329+
void Process_updateCmdline(Process* this, const char* cmdline, size_t basenameStart, size_t basenameEnd);
330330
void Process_updateExe(Process* this, const char* exe);
331331

332332
/* This function constructs the string that is displayed by

darwin/DarwinProcess.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
235235
/* Save where the argv[0] string starts. */
236236
sp = cp;
237237

238-
int end = 0;
238+
size_t end = 0;
239239
for ( np = NULL; c < nargs && cp < &procargs[size]; cp++ ) {
240240
if ( *cp == '\0' ) {
241241
c++;
@@ -246,7 +246,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
246246
/* Note location of current '\0'. */
247247
np = cp;
248248
if (end == 0) {
249-
end = cp - sp;
249+
end = (size_t)(cp - sp);
250250
}
251251
}
252252
}
@@ -260,7 +260,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
260260
goto ERROR_B;
261261
}
262262
if (end == 0) {
263-
end = np - sp;
263+
end = (size_t)(np - sp);
264264
}
265265

266266
Process_updateCmdline(proc, sp, 0, end);

dragonflybsd/DragonFlyBSDProcessTable.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ static void DragonFlyBSDProcessTable_updateProcessName(kvm_t* kd, const struct k
106106
}
107107

108108
size_t len = 0;
109-
for (int i = 0; argv[i]; i++) {
109+
for (size_t i = 0; argv[i]; i++) {
110110
len += strlen(argv[i]) + 1;
111111
}
112112

113113
char* cmdline = xMalloc(len);
114114

115115
char* at = cmdline;
116-
int end = 0;
117-
for (int i = 0; argv[i]; i++) {
116+
size_t end = 0;
117+
for (size_t i = 0; argv[i]; i++) {
118118
at = stpcpy(at, argv[i]);
119119
if (end == 0) {
120-
end = at - cmdline;
120+
end = (size_t)(at - cmdline);
121121
}
122122
*at++ = ' ';
123123
}

freebsd/FreeBSDProcessTable.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ static void FreeBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_
108108
}
109109

110110
size_t len = 0;
111-
for (int i = 0; argv[i]; i++) {
111+
for (size_t i = 0; argv[i]; i++) {
112112
len += strlen(argv[i]) + 1;
113113
}
114114

115115
char* cmdline = xMalloc(len);
116116
char* at = cmdline;
117-
int end = 0;
118-
for (int i = 0; argv[i]; i++) {
117+
size_t end = 0;
118+
for (size_t i = 0; argv[i]; i++) {
119119
at = stpcpy(at, argv[i]);
120120
if (end == 0) {
121-
end = at - cmdline;
121+
end = (size_t)(at - cmdline);
122122
}
123123
*at++ = ' ';
124124
}

0 commit comments

Comments
 (0)