1/ Use a "size_t num" instead of an "unsigned num" (also, do not store
the return value of sscanf() into num, it was unused anyway).
2/ Check the return value of strchr() and strrchr().
3/ Never jump over the terminating null byte with "S = tmp + 2".
// Reads /proc/*/stat files, being careful not to trip over processes with
// names like ":-) 1 2 3 4 5 6".
static void stat2proc(const char* S, proc_t *restrict P) {
- unsigned num;
+ size_t num;
char* tmp;
ENTER(0x160);
P->sched = -1;
P->nlwp = 0;
- S = strchr(S, '(') + 1;
+ S = strchr(S, '(');
+ if(unlikely(!S)) return;
+ S++;
tmp = strrchr(S, ')');
+ if(unlikely(!tmp)) return;
+ if(unlikely(!tmp[1])) return;
num = tmp - S;
if(unlikely(num >= sizeof P->cmd)) num = sizeof P->cmd - 1;
memcpy(P->cmd, S, num);
P->cmd[num] = '\0';
S = tmp + 2; // skip ") "
- num = sscanf(S,
+ sscanf(S,
"%c "
"%d %d %d %d %d "
"%lu %lu %lu %lu %lu "