Skip to content

Commit

Permalink
Avoid UB for deep nested processes
Browse files Browse the repository at this point in the history
Also increase the limit for nesting by using 64 bit integers.

ProcessList.c:242:36: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
    #0 0x561cfec000a8 in ProcessList_buildTreeBranch ProcessList.c:242:36
    #1 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    #2 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#3 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#4 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#5 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#6 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#7 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#8 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#9 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#10 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#11 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#12 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#13 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#14 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#15 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#16 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#17 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#18 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#19 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#20 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#21 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#22 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#23 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#24 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#25 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#26 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#27 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#28 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#29 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#30 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#31 0x561cfebffec2 in ProcessList_buildTreeBranch ProcessList.c:243:7
    htop-dev#32 0x561cfebfb734 in ProcessList_buildTree ProcessList.c:312:10
    htop-dev#33 0x561cfebfb050 in ProcessList_updateDisplayList ProcessList.c:326:10
    htop-dev#34 0x561cfebfc58b in ProcessList_rebuildPanel ProcessList.c:374:4
    htop-dev#35 0x561cfec07953 in checkRecalculation ScreenManager.c:139:7
    htop-dev#36 0x561cfec07953 in ScreenManager_run ScreenManager.c:226:10
    htop-dev#37 0x561cfeb8899a in CommandLine_run CommandLine.c:378:4
    htop-dev#38 0x561cfeb6d6d4 in main htop.c:15:11
    htop-dev#39 0x7f14860291e9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    htop-dev#40 0x7f148602929b in __libc_start_main csu/../csu/libc-start.c:392:3
    htop-dev#41 0x561cfeaafb20 in _start (htop+0x105b20) (BuildId: fc4b9e52ffc111ca8b4cd53136a238414120a858)
  • Loading branch information
cgzones committed Jun 28, 2022
1 parent d5b2785 commit c49183d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,9 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
char* buf = buffer;
const bool lastItem = (this->indent < 0);

for (int indent = (this->indent < 0 ? -this->indent : this->indent); indent > 1; indent >>= 1) {
for (uint64_t indent = (this->indent < 0 ? -this->indent : this->indent); indent > 1; indent >>= 1) {
int written, ret;
if (indent & 1) {
if (indent & 1U) {
ret = xSnprintf(buf, n, "%s ", CRT_treeStr[TREE_STR_VERT]);
} else {
ret = xSnprintf(buf, n, " ");
Expand Down
2 changes: 1 addition & 1 deletion Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ typedef struct Process_ {
/*
* Internal state for tree-mode.
*/
int indent;
int64_t indent;
unsigned int tree_depth;

/* Has no known parent process */
Expand Down
4 changes: 2 additions & 2 deletions ProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static void ProcessList_removeIndex(ProcessList* this, const Process* p, int idx
assert(Vector_countEquals(this->processes, Hashtable_count(this->processTable)));
}

static void ProcessList_buildTreeBranch(ProcessList* this, pid_t pid, int level, int indent, bool show) {
static void ProcessList_buildTreeBranch(ProcessList* this, pid_t pid, unsigned int level, int64_t indent, bool show) {
// On OpenBSD the kernel thread 'swapper' has pid 0.
// Do not treat it as root of any tree.
if (pid == 0)
Expand Down Expand Up @@ -239,7 +239,7 @@ static void ProcessList_buildTreeBranch(ProcessList* this, pid_t pid, int level,

Vector_add(this->displayList, process);

int nextIndent = indent | (1 << level);
int64_t nextIndent = indent | ((int64_t)1 << MINIMUM(level, sizeof(process->indent) * 8 - 2));
ProcessList_buildTreeBranch(this, process->pid, level + 1, (i < lastShown) ? nextIndent : indent, process->show && process->showChildren);
if (i == lastShown) {
process->indent = -nextIndent;
Expand Down

0 comments on commit c49183d

Please sign in to comment.