Skip to content

Commit 613c74b

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 469dbbc commit 613c74b

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] == ':') {
@@ -875,7 +873,7 @@ bool Process_rowMatchesFilter(const Row* super, const Table* table) {
875873
void Process_init(Process* this, const Machine* host) {
876874
Row_init(&this->super, host);
877875

878-
this->cmdlineBasenameEnd = -1;
876+
this->cmdlineBasenameEnd = 0;
879877
this->st_uid = (uid_t)-1;
880878
}
881879

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

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

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

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

10571054
if (!this->cmdline && !cmdline)
10581055
return;
@@ -1085,7 +1082,7 @@ void Process_updateExe(Process* this, const char* exe) {
10851082
if (exe) {
10861083
this->procExe = xStrdup(exe);
10871084
const char* lastSlash = strrchr(exe, '/');
1088-
this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0;
1085+
this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (size_t)(lastSlash - exe + 1) : 0;
10891086
} else {
10901087
this->procExe = NULL;
10911088
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
@@ -233,7 +233,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
233233
/* Save where the argv[0] string starts. */
234234
sp = cp;
235235

236-
int end = 0;
236+
size_t end = 0;
237237
for ( np = NULL; c < nargs && cp < &procargs[size]; cp++ ) {
238238
if ( *cp == '\0' ) {
239239
c++;
@@ -244,7 +244,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
244244
/* Note location of current '\0'. */
245245
np = cp;
246246
if (end == 0) {
247-
end = cp - sp;
247+
end = (size_t)(cp - sp);
248248
}
249249
}
250250
}
@@ -258,7 +258,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro
258258
goto ERROR_B;
259259
}
260260
if (end == 0) {
261-
end = np - sp;
261+
end = (size_t)(np - sp);
262262
}
263263

264264
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)