diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0beedc3c1..b0582dfc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,16 +31,16 @@ jobs: build-ubuntu-latest-minimal-clang: runs-on: ubuntu-latest env: - CC: clang-18 + CC: clang-19 steps: - uses: actions/checkout@v4 - name: install clang repo run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | sudo apt-key add - - sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' -y + sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-19 main' -y sudo apt-get update -q - name: Install Dependencies - run: sudo apt-get install --no-install-recommends clang-18 libncursesw5-dev + run: sudo apt-get install --no-install-recommends clang-19 libncursesw5-dev - name: Bootstrap run: ./autogen.sh - name: Configure @@ -72,16 +72,16 @@ jobs: build-ubuntu-latest-full-featured-clang: runs-on: ubuntu-latest env: - CC: clang-18 + CC: clang-19 steps: - uses: actions/checkout@v4 - name: install clang repo run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | sudo apt-key add - - sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' -y + sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-19 main' -y sudo apt-get update -q - name: Install Dependencies - run: sudo apt-get install --no-install-recommends clang-18 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev libcap-dev + run: sudo apt-get install --no-install-recommends clang-19 libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev libcap-dev - name: Bootstrap run: ./autogen.sh - name: Configure @@ -127,27 +127,27 @@ jobs: build-ubuntu-latest-clang-analyzer: runs-on: ubuntu-latest env: - CC: clang-18 + CC: clang-19 steps: - uses: actions/checkout@v4 - name: install clang repo run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | sudo apt-key add - - sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' -y + sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-19 main' -y sudo apt-get update -q - name: Install Dependencies - run: sudo apt-get install --no-install-recommends clang-18 clang-tools-18 libncursesw5-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev libcap-dev + run: sudo apt-get install --no-install-recommends clang-19 clang-tools-19 libncursesw5-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev libcap-dev - name: Bootstrap run: ./autogen.sh - name: Configure - run: scan-build-18 -analyze-headers --status-bugs ./configure --enable-debug --enable-werror --enable-openvz --enable-vserver --enable-ancient-vserver --enable-unicode --enable-delayacct --enable-sensors --enable-capabilities || ( cat config.log; exit 1; ) + run: scan-build-19 -analyze-headers --status-bugs ./configure --enable-debug --enable-werror --enable-openvz --enable-vserver --enable-ancient-vserver --enable-unicode --enable-delayacct --enable-sensors --enable-capabilities || ( cat config.log; exit 1; ) - name: Build - run: scan-build-18 -analyze-headers --status-bugs make -j"$(nproc)" + run: scan-build-19 -analyze-headers --status-bugs make -j"$(nproc)" build-ubuntu-latest-clang-sanitizer: runs-on: ubuntu-latest env: - CC: clang-18 + CC: clang-19 CFLAGS: '-O1 -g -ftrivial-auto-var-init=pattern -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-address-use-after-return=always -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=nullability -fsanitize=implicit-conversion -fsanitize=integer -fsanitize=float-divide-by-zero -fsanitize=local-bounds' LDFLAGS: '-ftrivial-auto-var-init=pattern -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-address-use-after-return=always -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=nullability -fsanitize=implicit-conversion -fsanitize=integer -fsanitize=float-divide-by-zero -fsanitize=local-bounds' ASAN_OPTIONS: strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1 @@ -159,10 +159,10 @@ jobs: - name: install clang repo run: | wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key 2>/dev/null | sudo apt-key add - - sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' -y + sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-19 main' -y sudo apt-get update -q - name: Install LLVM Toolchain - run: sudo apt-get install --no-install-recommends clang-18 libclang-rt-18-dev llvm-18 + run: sudo apt-get install --no-install-recommends clang-19 libclang-rt-19-dev llvm-19 - name: Install Dependencies run: sudo apt-get install --no-install-recommends libncursesw5-dev libhwloc-dev libnl-3-dev libnl-genl-3-dev libsensors4-dev libcap-dev - name: Bootstrap diff --git a/Action.c b/Action.c index 56fb3d0cb..383199ca8 100644 --- a/Action.c +++ b/Action.c @@ -401,15 +401,19 @@ static Htop_Reaction actionPrevScreen(State* st) { } Htop_Reaction Action_setScreenTab(State* st, int x) { + assert(x >= 0); + if (x < 0) + return 0; + unsigned int pos = x; Settings* settings = st->host->settings; - int s = 2; + unsigned int s = 2; for (unsigned int i = 0; i < settings->nScreens; i++) { - if (x < s) { + if (pos < s) { return 0; } const char* tab = settings->screens[i]->heading; - int len = strlen(tab); - if (x <= s + len + 1) { + size_t len = strlen(tab); + if (pos <= s + len + 1) { settings->ssIndex = i; setActiveScreen(settings, st, i); return HTOP_UPDATE_PANELHDR | HTOP_REFRESH | HTOP_REDRAW_BAR; diff --git a/AffinityPanel.c b/AffinityPanel.c index f4009080b..cd9cdffcb 100644 --- a/AffinityPanel.c +++ b/AffinityPanel.c @@ -311,7 +311,7 @@ static MaskItem* AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, u } /* "[x] " + "|- " * depth + ("- ")?(if root node) + name */ - unsigned width = 4 + 3 * depth + (2 * !depth) + strlen(buf); + unsigned width = 4 + 3 * depth + (2 * !depth) + (unsigned)strlen(buf); if (width > this->width) { this->width = width; } @@ -389,7 +389,7 @@ Panel* AffinityPanel_new(Machine* host, const Affinity* affinity, int* width) { char number[16]; xSnprintf(number, 9, "CPU %d", Settings_cpuId(host->settings, i)); - unsigned cpu_width = 4 + strlen(number); + unsigned cpu_width = 4 + (unsigned) strlen(number); if (cpu_width > this->width) { this->width = cpu_width; } diff --git a/CommandLine.c b/CommandLine.c index b93a0f969..c4d7116e7 100644 --- a/CommandLine.c +++ b/CommandLine.c @@ -12,6 +12,7 @@ in the source distribution for its full text. #include #include +#include #include #include #include @@ -204,12 +205,15 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin if (!username) { flags->userId = geteuid(); } else if (!Action_setUserOnly(username, &(flags->userId))) { - for (const char* itr = username; *itr; ++itr) - if (!isdigit((unsigned char)*itr)) { - fprintf(stderr, "Error: invalid user \"%s\".\n", username); - return STATUS_ERROR_EXIT; - } - flags->userId = atol(username); + char *endptr; + errno = 0; + unsigned long res = strtoul(username, &endptr, 10); + unsigned castRes = (unsigned) res; + if (*endptr != '\0' || res == ULONG_MAX || errno != 0 || castRes != res) { + fprintf(stderr, "Error: invalid user \"%s\".\n", username); + return STATUS_ERROR_EXIT; + } + flags->userId = castRes; } break; } diff --git a/DynamicMeter.c b/DynamicMeter.c index d0f20a272..79c03f111 100644 --- a/DynamicMeter.c +++ b/DynamicMeter.c @@ -106,6 +106,7 @@ static void DynamicMeter_getUiName(const Meter* this, char* name, size_t length) const char* uiName = meter->caption; if (uiName) { size_t uiNameLen = strlen(uiName); + assert(uiNameLen < 32); if (uiNameLen > 2 && uiName[uiNameLen - 2] == ':') uiNameLen -= 2; diff --git a/Header.c b/Header.c index 8a9eae34d..fa5b864e4 100644 --- a/Header.c +++ b/Header.c @@ -77,7 +77,7 @@ void Header_setLayout(Header* this, HeaderLayout hLayout) { Header_calculateHeight(this); } -static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, unsigned int column) { +static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, size_t column) { assert(column < HeaderLayout_getColumns(this->headerLayout)); Vector* meters = this->columns[column]; @@ -170,7 +170,7 @@ void Header_writeBackToSettings(const Header* this) { } } -Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column) { +Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, size_t column) { assert(column < HeaderLayout_getColumns(this->headerLayout)); Vector* meters = this->columns[column]; @@ -198,8 +198,8 @@ void Header_draw(const Header* this) { for (int y = 0; y < height; y++) { mvhline(y, 0, ' ', COLS); } - const int numCols = HeaderLayout_getColumns(this->headerLayout); - const int width = COLS - 2 * pad - (numCols - 1); + const size_t numCols = HeaderLayout_getColumns(this->headerLayout); + const size_t width = COLS - 2 * pad - (numCols - 1); int x = pad; float roundingLoss = 0.0F; @@ -221,7 +221,7 @@ void Header_draw(const Header* this) { /* Let meters in text mode expand to the right on empty neighbors; except for multi column meters. */ if (meter->mode == TEXT_METERMODE && !Meter_isMultiColumn(meter)) { - for (int j = 1; j < meter->columnWidthCount; j++) { + for (size_t j = 1; j < meter->columnWidthCount; j++) { actualWidth++; /* separator column */ actualWidth += (float)width * HeaderLayout_layouts[this->headerLayout].widths[col + j] / 100.0F; } @@ -253,7 +253,7 @@ void Header_updateData(Header* this) { * by counting how many columns to the right are empty or contain a BlankMeter. * Returns the number of columns to span, i.e. if the direct neighbor is occupied 1. */ -static int calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const unsigned int curColumn, const int curHeight) { +static size_t calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const size_t curColumn, const int curHeight) { for (size_t i = curColumn + 1; i < HeaderLayout_getColumns(this->headerLayout); i++) { const Vector* meters = this->columns[i]; diff --git a/Header.h b/Header.h index 2cc78ab72..b0ffd5921 100644 --- a/Header.h +++ b/Header.h @@ -35,7 +35,7 @@ void Header_populateFromSettings(Header* this); void Header_writeBackToSettings(const Header* this); -Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column); +Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, size_t column); void Header_reinit(Header* this); diff --git a/IncSet.h b/IncSet.h index a84407e50..28ecc6772 100644 --- a/IncSet.h +++ b/IncSet.h @@ -24,7 +24,7 @@ typedef enum { typedef struct IncMode_ { char buffer[INCMODE_MAX + 1]; - int index; + size_t index; FunctionBar* bar; bool isFilter; } IncMode; diff --git a/Meter.c b/Meter.c index 4463a90a0..3b5ace38a 100644 --- a/Meter.c +++ b/Meter.c @@ -54,7 +54,7 @@ static void TextMeterMode_draw(Meter* this, int x, int y, int w) { mvaddnstr(y, x, caption, w); attrset(CRT_colors[RESET_COLOR]); - int captionLen = strlen(caption); + int captionLen = (int)strlen(caption); x += captionLen; w -= captionLen; if (w <= 0) @@ -251,7 +251,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { // Starting positions of graph data and terminal column if ((size_t)w > nValues / 2) { x += w - nValues / 2; - w = nValues / 2; + w = (int)(nValues / 2); } size_t i = nValues - (size_t)w * 2; @@ -319,7 +319,9 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { attrset(CRT_colors[LED_COLOR]); const char* caption = Meter_getCaption(this); mvaddstr(yText, x, caption); - int xx = x + strlen(caption); + size_t capLen = strlen(caption); + assert(capLen < 32); + int xx = x + (int) MINIMUM(capLen, 32); int len = RichString_sizeVal(out); for (int i = 0; i < len; i++) { int c = RichString_getCharVal(out, i); diff --git a/Meter.h b/Meter.h index b53a82324..ec7975c1b 100644 --- a/Meter.h +++ b/Meter.h @@ -112,7 +112,7 @@ struct Meter_ { unsigned int param; GraphData drawData; int h; - int columnWidthCount; /**< only used internally by the Header */ + size_t columnWidthCount; /**< only used internally by the Header */ uint8_t curItems; const int* curAttributes; char txtBuffer[METER_TXTBUFFER_LEN]; diff --git a/Panel.c b/Panel.c index 4784a6585..be31d2aef 100644 --- a/Panel.c +++ b/Panel.c @@ -455,7 +455,7 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) { char* buffer = this->eventHandlerState; if (0 < ch && ch < 255 && isgraph((unsigned char)ch)) { - int len = strlen(buffer); + size_t len = strlen(buffer); if (!len) { if ('/' == ch) { ch = '\001'; diff --git a/Process.c b/Process.c index 910443e99..7482ae604 100644 --- a/Process.c +++ b/Process.c @@ -85,8 +85,8 @@ static bool findCommInCmdline(const char* comm, const char* cmdline, int cmdline if ((tokenLen == commLen || (tokenLen > commLen && commLen == (TASK_COMM_LEN - 1))) && strncmp(tokenBase, comm, commLen) == 0) { - *pCommStart = tokenBase - cmdline; - *pCommEnd = token - cmdline; + *pCommStart = (int)(tokenBase - cmdline); + *pCommEnd = (int)(token - cmdline); return true; } @@ -209,7 +209,7 @@ void Process_makeCommandStr(Process* this, const Settings* settings) { /* The field separator "│" has been chosen such that it will not match any * valid string used for searching or filtering */ const char* SEPARATOR = CRT_treeStr[TREE_STR_VERT]; - const int SEPARATOR_LEN = strlen(SEPARATOR); + const size_t SEPARATOR_LEN = strlen(SEPARATOR); /* Accommodate the column text, two field separators and terminating NUL */ size_t maxLen = 2 * SEPARATOR_LEN + 1; @@ -350,7 +350,7 @@ void Process_makeCommandStr(Process* this, const Settings* settings) { return; } - int exeLen = strlen(this->procExe); + int exeLen = (int)MINIMUM(strlen(this->procExe), 65536); int exeBasenameOffset = this->procExeBasenameOffset; int exeBasenameLen = exeLen - exeBasenameOffset; @@ -509,7 +509,7 @@ void Process_writeCommand(const Process* this, int attr, int baseAttr, RichStrin if (!highlightDeleted) continue; - RichString_setAttrn(str, hl->attr, strStart + hl->offset, hl->length); + RichString_setAttrn(str, hl->attr, (int)(strStart + hl->offset), (int)hl->length); } } @@ -575,7 +575,7 @@ void Process_writeField(const Process* this, RichString* str, RowField field) { ret = xSnprintf(buf, n, " "); } if (ret < 0 || (size_t)ret >= n) { - written = n; + written = (int)n; } else { written = ret; } @@ -870,7 +870,13 @@ static bool Process_setPriority(Process* this, int priority) { bool Process_rowChangePriorityBy(Row* super, Arg delta) { Process* this = (Process*) super; assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class)); - return Process_setPriority(this, this->nice + delta.i); + + long value = this->nice + delta.i; + int prio = (int)value; + if (prio != value) + return false; + + return Process_setPriority(this, prio); } static bool Process_sendSignal(Process* this, Arg sgn) { @@ -1002,12 +1008,12 @@ void Process_updateComm(Process* this, const char* comm) { this->mergedCommand.lastUpdate = 0; } -static int skipPotentialPath(const char* cmdline, int end) { +static size_t skipPotentialPath(const char* cmdline, size_t end) { if (cmdline[0] != '/') return 0; - int slash = 0; - for (int i = 1; i < end; i++) { + size_t slash = 0; + for (size_t i = 1; i < end; i++) { if (cmdline[i] == '/' && cmdline[i + 1] != '\0') { slash = i + 1; continue; @@ -1023,11 +1029,10 @@ static int skipPotentialPath(const char* cmdline, int end) { return slash; } -void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd) { - assert(basenameStart >= 0); - assert((cmdline && basenameStart < (int)strlen(cmdline)) || (!cmdline && basenameStart == 0)); +void Process_updateCmdline(Process* this, const char* cmdline, size_t basenameStart, size_t basenameEnd) { + assert((cmdline && basenameStart < strlen(cmdline)) || (!cmdline && basenameStart == 0)); assert((basenameEnd > basenameStart) || (basenameEnd == 0 && basenameStart == 0)); - assert((cmdline && basenameEnd <= (int)strlen(cmdline)) || (!cmdline && basenameEnd == 0)); + assert((cmdline && basenameEnd <= strlen(cmdline)) || (!cmdline && basenameEnd == 0)); if (!this->cmdline && !cmdline) return; @@ -1035,6 +1040,9 @@ void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart if (this->cmdline && cmdline && String_eq(this->cmdline, cmdline)) return; + basenameStart = MINIMUM(basenameStart, 1000000); + basenameEnd = MINIMUM(basenameEnd, 1000000); + free(this->cmdline); this->cmdline = cmdline ? xStrdup(cmdline) : NULL; if (Process_isKernelThread(this)) { @@ -1042,8 +1050,8 @@ void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart this->cmdlineBasenameStart = 0; this->cmdlineBasenameEnd = 0; } else { - this->cmdlineBasenameStart = (basenameStart || !cmdline) ? basenameStart : skipPotentialPath(cmdline, basenameEnd); - this->cmdlineBasenameEnd = basenameEnd; + this->cmdlineBasenameStart = (basenameStart || !cmdline) ? (int)basenameStart : (int)skipPotentialPath(cmdline, basenameEnd); + this->cmdlineBasenameEnd = (int)basenameEnd; } this->mergedCommand.lastUpdate = 0; @@ -1060,7 +1068,8 @@ void Process_updateExe(Process* this, const char* exe) { if (exe) { this->procExe = xStrdup(exe); const char* lastSlash = strrchr(exe, '/'); - this->procExeBasenameOffset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0; + long offset = (lastSlash && *(lastSlash + 1) != '\0' && lastSlash != exe) ? (lastSlash - exe + 1) : 0; + this->procExeBasenameOffset = MAXIMUM((int)offset, 0); } else { this->procExe = NULL; this->procExeBasenameOffset = 0; diff --git a/Process.h b/Process.h index b7b48cb1b..f439b8797 100644 --- a/Process.h +++ b/Process.h @@ -326,7 +326,7 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField const char* Process_getCommand(const Process* this); void Process_updateComm(Process* this, const char* comm); -void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd); +void Process_updateCmdline(Process* this, const char* cmdline, size_t basenameStart, size_t basenameEnd); void Process_updateExe(Process* this, const char* exe); /* This function constructs the string that is displayed by diff --git a/RichString.c b/RichString.c index 390beb073..63706538a 100644 --- a/RichString.c +++ b/RichString.c @@ -50,6 +50,7 @@ static void RichString_extendLen(RichString* this, int len) { } static void RichString_setLen(RichString* this, int len) { + len = MAXIMUM(len, 0); if (len < RICHSTRING_MAXLEN && this->chlen < RICHSTRING_MAXLEN) { RichString_setChar(this, len, 0); this->chlen = len; @@ -59,7 +60,7 @@ static void RichString_setLen(RichString* this, int len) { } void RichString_rewind(RichString* this, int count) { - RichString_setLen(this, this->chlen - count); + RichString_setLen(this, CLAMP(this->chlen - count, 0, this->chlen)); } #ifdef HAVE_LIBNCURSESW @@ -99,7 +100,7 @@ static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src static inline int RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) { wchar_t data[len]; - len = mbstowcs_nonfatal(data, data_c, len); + len = (int)mbstowcs_nonfatal(data, data_c, len); if (len <= 0) return 0; @@ -114,7 +115,7 @@ static inline int RichString_writeFromWide(RichString* this, int attrs, const ch int RichString_appendnWideColumns(RichString* this, int attrs, const char* data_c, int len, int* columns) { wchar_t data[len]; - len = mbstowcs_nonfatal(data, data_c, len); + len = (int)mbstowcs_nonfatal(data, data_c, len); if (len <= 0) return 0; @@ -243,7 +244,7 @@ void RichString_setAttr(RichString* this, int attrs) { } int RichString_appendWide(RichString* this, int attrs, const char* data) { - return RichString_writeFromWide(this, attrs, data, this->chlen, strlen(data)); + return RichString_writeFromWide(this, attrs, data, this->chlen, (int)strlen(data)); } int RichString_appendnWide(RichString* this, int attrs, const char* data, int len) { @@ -251,11 +252,11 @@ int RichString_appendnWide(RichString* this, int attrs, const char* data, int le } int RichString_writeWide(RichString* this, int attrs, const char* data) { - return RichString_writeFromWide(this, attrs, data, 0, strlen(data)); + return RichString_writeFromWide(this, attrs, data, 0, (int)strlen(data)); } int RichString_appendAscii(RichString* this, int attrs, const char* data) { - return RichString_writeFromAscii(this, attrs, data, this->chlen, strlen(data)); + return RichString_writeFromAscii(this, attrs, data, this->chlen, (int)strlen(data)); } int RichString_appendnAscii(RichString* this, int attrs, const char* data, int len) { @@ -263,5 +264,5 @@ int RichString_appendnAscii(RichString* this, int attrs, const char* data, int l } int RichString_writeAscii(RichString* this, int attrs, const char* data) { - return RichString_writeFromAscii(this, attrs, data, 0, strlen(data)); + return RichString_writeFromAscii(this, attrs, data, 0, (int)strlen(data)); } diff --git a/Row.c b/Row.c index d795787e6..64112325e 100644 --- a/Row.c +++ b/Row.c @@ -181,7 +181,7 @@ RowField RowField_keyAt(const Settings* settings, int at) { RowField field; int x = 0; for (int i = 0; (field = fields[i]); i++) { - int len = strlen(RowField_alignedTitle(settings, field)); + int len = (int)strlen(RowField_alignedTitle(settings, field)); if (at >= x && at <= x + len) { return field; } @@ -428,22 +428,22 @@ void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds, } unsigned long long totalSeconds = totalMicroseconds / 1000000; - unsigned long microseconds = totalMicroseconds % 1000000; + unsigned int microseconds = totalMicroseconds % 1000000; if (totalSeconds < 60) { int width = 5; - unsigned long fraction = microseconds / 10; + unsigned int fraction = microseconds / 10; if (totalSeconds >= 10) { width--; fraction /= 10; } - len = xSnprintf(buffer, sizeof(buffer), "%u.%0*lus ", (unsigned int)totalSeconds, width, fraction); + len = xSnprintf(buffer, sizeof(buffer), "%u.%0*us ", (unsigned int)totalSeconds, width, fraction); RichString_appendnAscii(str, baseColor, buffer, len); return; } if (totalSeconds < 600) { - unsigned int minutes = totalSeconds / 60; - unsigned int seconds = totalSeconds % 60; + unsigned int minutes = (unsigned int)totalSeconds / 60; + unsigned int seconds = (unsigned int)totalSeconds % 60; unsigned int milliseconds = microseconds / 1000; len = xSnprintf(buffer, sizeof(buffer), "%u:%02u.%03u ", minutes, seconds, milliseconds); RichString_appendnAscii(str, baseColor, buffer, len); @@ -495,7 +495,8 @@ void Row_printRate(RichString* str, double rate, bool coloring) { void Row_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width) { int columns = width; - RichString_appendnWideColumns(str, attr, content, strlen(content), &columns); + size_t contentLen = strlen(content); + RichString_appendnWideColumns(str, attr, content, (int)MINIMUM(contentLen, 1000), &columns); RichString_appendChr(str, attr, ' ', width + 1 - columns); } diff --git a/ScreenManager.c b/ScreenManager.c index 6f3b0598f..57de35e5c 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -166,8 +166,9 @@ static inline bool drawTab(const int* y, int* x, int l, const char* name, bool c (*x)++; if (*x >= l) return false; - int nameLen = strlen(name); - int n = MINIMUM(l - *x, nameLen); + size_t nameLen = strlen(name); + assert(nameLen < 100); + int n = MINIMUM(l - *x, (int)nameLen); attrset(CRT_colors[cur ? SCREENS_CUR_TEXT : SCREENS_OTH_TEXT]); mvaddnstr(*y, *x, name, n); *x += n; diff --git a/ScreenTabsPanel.c b/ScreenTabsPanel.c index e48e5fb8b..45a5e7388 100644 --- a/ScreenTabsPanel.c +++ b/ScreenTabsPanel.c @@ -210,7 +210,7 @@ static HandlerResult ScreenNamesPanel_eventHandlerRenaming(Panel* super, int ch) if (this->cursor < SCREEN_NAME_LEN - 1) { this->buffer[this->cursor] = (char)ch; this->cursor++; - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } @@ -223,7 +223,7 @@ static HandlerResult ScreenNamesPanel_eventHandlerRenaming(Panel* super, int ch) if (this->cursor > 0) { this->cursor--; this->buffer[this->cursor] = '\0'; - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } break; @@ -271,10 +271,10 @@ static void startRenaming(Panel* super) { this->saved = name; strncpy(this->buffer, name, SCREEN_NAME_LEN); this->buffer[SCREEN_NAME_LEN] = '\0'; - this->cursor = strlen(this->buffer); + this->cursor = (int) strlen(this->buffer); item->value = this->buffer; Panel_setSelectionColor(super, PANEL_EDIT); - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } diff --git a/ScreensPanel.c b/ScreensPanel.c index 4138066b5..8d2b77ec5 100644 --- a/ScreensPanel.c +++ b/ScreensPanel.c @@ -69,7 +69,7 @@ static HandlerResult ScreensPanel_eventHandlerRenaming(Panel* super, int ch) { if (this->cursor < SCREEN_NAME_LEN - 1) { this->buffer[this->cursor] = (char)ch; this->cursor++; - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } @@ -82,7 +82,7 @@ static HandlerResult ScreensPanel_eventHandlerRenaming(Panel* super, int ch) { if (this->cursor > 0) { this->cursor--; this->buffer[this->cursor] = '\0'; - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } break; @@ -129,10 +129,10 @@ static void startRenaming(Panel* super) { this->saved = name; strncpy(this->buffer, name, SCREEN_NAME_LEN); this->buffer[SCREEN_NAME_LEN] = '\0'; - this->cursor = strlen(this->buffer); + this->cursor = (int) strlen(this->buffer); item->value = this->buffer; Panel_setSelectionColor(super, PANEL_EDIT); - super->selectedLen = strlen(this->buffer); + super->selectedLen = (int) strlen(this->buffer); Panel_setCursorToSelection(super); } diff --git a/Settings.c b/Settings.c index 92cd85427..305689274 100644 --- a/Settings.c +++ b/Settings.c @@ -585,9 +585,9 @@ static void writeFields(OutputFunc of, FILE* fp, } static void writeList(OutputFunc of, FILE* fp, - char** list, int len, char separator) { + char** list, size_t len, char separator) { const char* sep = ""; - for (int i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { of(fp, "%s%s", sep, list[i]); sep = " "; } @@ -630,7 +630,7 @@ static int signal_safe_fprintf(FILE* stream, const char* fmt, ...) { if (n <= 0) return n; - return full_write_str(fileno(stream), buf); + return (int) full_write_str(fileno(stream), buf); } int Settings_write(const Settings* this, bool onCrash) { @@ -917,14 +917,14 @@ bool Settings_isReadonly(void) { } void Settings_setHeaderLayout(Settings* this, HeaderLayout hLayout) { - unsigned int oldColumns = HeaderLayout_getColumns(this->hLayout); - unsigned int newColumns = HeaderLayout_getColumns(hLayout); + size_t oldColumns = HeaderLayout_getColumns(this->hLayout); + size_t newColumns = HeaderLayout_getColumns(hLayout); if (newColumns > oldColumns) { this->hColumns = xReallocArray(this->hColumns, newColumns, sizeof(MeterColumnSetting)); memset(this->hColumns + oldColumns, 0, (newColumns - oldColumns) * sizeof(MeterColumnSetting)); } else if (newColumns < oldColumns) { - for (unsigned int i = newColumns; i < oldColumns; i++) { + for (size_t i = newColumns; i < oldColumns; i++) { if (this->hColumns[i].names) { for (size_t j = 0; j < this->hColumns[i].len; j++) free(this->hColumns[i].names[j]); diff --git a/Vector.c b/Vector.c index aeb193917..c1dc622ed 100644 --- a/Vector.c +++ b/Vector.c @@ -69,8 +69,8 @@ static bool Vector_isConsistent(const Vector* this) { return true; } -bool Vector_countEquals(const Vector* this, unsigned int expectedCount) { - unsigned int n = 0; +bool Vector_countEquals(const Vector* this, size_t expectedCount) { + size_t n = 0; for (int i = 0; i < this->items; i++) { if (this->array[i]) { n++; diff --git a/Vector.h b/Vector.h index b7b39031e..7c69d58ed 100644 --- a/Vector.h +++ b/Vector.h @@ -72,7 +72,7 @@ int Vector_size(const Vector* this); /* Vector_countEquals returns true if the number of non-NULL items in the Vector is equal to expectedCount. This is only for debugging and consistency checks. */ -bool Vector_countEquals(const Vector* this, unsigned int expectedCount); +bool Vector_countEquals(const Vector* this, size_t expectedCount); #else /* NDEBUG */ diff --git a/linux/LinuxMachine.c b/linux/LinuxMachine.c index f219cd61c..a998b6473 100644 --- a/linux/LinuxMachine.c +++ b/linux/LinuxMachine.c @@ -74,7 +74,7 @@ static void LinuxMachine_updateCPUcount(LinuxMachine* this) { char* endp; unsigned long int id = strtoul(entry->d_name + 3, &endp, 10); - if (id == ULONG_MAX || endp == entry->d_name + 3 || *endp != '\0') + if (id == UINT_MAX || endp == entry->d_name + 3 || *endp != '\0') continue; #ifdef HAVE_OPENAT @@ -89,7 +89,7 @@ static void LinuxMachine_updateCPUcount(LinuxMachine* this) { existing++; /* readdir() iterates with no specific order */ - unsigned int max = MAXIMUM(existing, id + 1); + unsigned int max = MAXIMUM(existing, (unsigned int)id + 1); if (max > currExisting) { this->cpuData = xReallocArrayZero(this->cpuData, currExisting ? (currExisting + 1) : 0, max + /* aggregate */ 1, sizeof(CPUData)); this->cpuData[0].online = true; /* average is always "online" */ @@ -656,7 +656,7 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) { Machine_init(super, usersTable, userId); // Initialize page size - if ((this->pageSize = sysconf(_SC_PAGESIZE)) == -1) + if ((this->pageSize = (int) sysconf(_SC_PAGESIZE)) == -1) CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)"); this->pageSizeKB = this->pageSize / ONE_K; diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index e70d7d0c9..d9e7ee64b 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -144,7 +144,7 @@ io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page */ static int LinuxProcess_effectiveIOPriority(const LinuxProcess* this) { if (IOPriority_class(this->ioPriority) == IOPRIO_CLASS_NONE) { - return IOPriority_tuple(IOPRIO_CLASS_BE, (this->super.nice + 20) / 5); + return IOPriority_tuple(IOPRIO_CLASS_BE, (int)((this->super.nice + 20) / 5)); } return this->ioPriority; @@ -162,7 +162,7 @@ IOPriority LinuxProcess_updateIOPriority(Process* p) { IOPriority ioprio = 0; // Other OSes masquerading as Linux (NetBSD?) don't have this syscall #ifdef SYS_ioprio_get - ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, Process_getPid(p)); + ioprio = (int) syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, Process_getPid(p)); #endif LinuxProcess* this = (LinuxProcess*) p; this->ioPriority = ioprio; diff --git a/linux/LinuxProcessTable.c b/linux/LinuxProcessTable.c index b1705904b..5211a572c 100644 --- a/linux/LinuxProcessTable.c +++ b/linux/LinuxProcessTable.c @@ -334,15 +334,15 @@ static bool LinuxProcessTable_readStatFile(LinuxProcess* lp, openat_arg_t procFd location += 2; /* (4) ppid - %d */ - Process_setParent(process, fast_strtol_dec(&location, 0)); + Process_setParent(process, (pid_t) fast_strtol_dec(&location, 0)); location += 1; /* (5) pgrp - %d */ - process->pgrp = fast_strtol_dec(&location, 0); + process->pgrp = (int) fast_strtol_dec(&location, 0); location += 1; /* (6) session - %d */ - process->session = fast_strtol_dec(&location, 0); + process->session = (int) fast_strtol_dec(&location, 0); location += 1; /* (7) tty_nr - %d */ @@ -350,7 +350,7 @@ static bool LinuxProcessTable_readStatFile(LinuxProcess* lp, openat_arg_t procFd location += 1; /* (8) tpgid - %d */ - process->tpgid = fast_strtol_dec(&location, 0); + process->tpgid = (int) fast_strtol_dec(&location, 0); location += 1; /* (9) flags - %u */ @@ -420,7 +420,7 @@ static bool LinuxProcessTable_readStatFile(LinuxProcess* lp, openat_arg_t procFd assert(location != NULL); /* (39) processor - %d */ - process->processor = fast_strtol_dec(&location, 0); + process->processor = (int) fast_strtol_dec(&location, 0); /* Ignore further fields */ @@ -648,7 +648,7 @@ static void LinuxProcessTable_readMaps(LinuxProcess* process, openat_arg_t procF bool map_execute; unsigned int map_devmaj; unsigned int map_devmin; - uint64_t map_inode; + unsigned int map_inode; // Short circuit test: Look for a slash if (!strchr(buffer, '/')) @@ -678,19 +678,19 @@ static void LinuxProcessTable_readMaps(LinuxProcess* process, openat_arg_t procF if (' ' != *readptr++) continue; - map_devmaj = fast_strtoull_hex(&readptr, 4); + map_devmaj = (unsigned int) fast_strtoull_hex(&readptr, 4); if (':' != *readptr++) continue; - map_devmin = fast_strtoull_hex(&readptr, 4); + map_devmin = (unsigned int) fast_strtoull_hex(&readptr, 4); if (' ' != *readptr++) continue; - //Minor shortcut: Once we know there's no file for this region, we skip + // Minor shortcut: Once we know there's no file for this region, we skip if (!map_devmaj && !map_devmin) continue; - map_inode = fast_strtoull_dec(&readptr, 0); + map_inode = (unsigned int) fast_strtoull_dec(&readptr, 0); if (!map_inode) continue; @@ -882,7 +882,7 @@ static void LinuxProcessTable_readOpenVZData(LinuxProcess* process, openat_arg_t break; case 2: foundVPid = true; - process->vpid = strtoul(name_value_sep, NULL, 0); + process->vpid = (pid_t)strtoul(name_value_sep, NULL, 0); break; default: //Sanity Check: Should never reach here, or the implementation is missing something! @@ -1011,13 +1011,13 @@ static void LinuxProcessTable_readOomData(LinuxProcess* process, openat_arg_t pr char buffer[PROC_LINE_LENGTH + 1] = {0}; - ssize_t oomRead = xReadfileat(procFd, "oom_score", buffer, sizeof(buffer)); + int oomRead = (int) xReadfileat(procFd, "oom_score", buffer, sizeof(buffer)); if (oomRead < 1) { return; } char* oomPtr = buffer; - uint64_t oom = fast_strtoull_dec(&oomPtr, oomRead); + unsigned long oom = fast_strtoul_dec(&oomPtr, oomRead); if (*oomPtr && *oomPtr != '\n' && *oomPtr != ' ') { return; } @@ -1026,7 +1026,7 @@ static void LinuxProcessTable_readOomData(LinuxProcess* process, openat_arg_t pr return; } - process->oom = oom; + process->oom = (unsigned int)oom; } /* @@ -1246,10 +1246,10 @@ static bool LinuxProcessTable_readCmdlineFile(Process* process, openat_arg_t pro tokenStart = -1; tokenEnd = -1; - size_t exeLen = process->procExe ? strlen(process->procExe) : 0; + int exeLen = process->procExe ? (int) strlen(process->procExe) : 0; if (process->procExe && String_startsWith(command, process->procExe) && - exeLen < (size_t)lastChar && command[exeLen] <= ' ') { + exeLen < lastChar && command[exeLen] <= ' ') { tokenStart = process->procExeBasenameOffset; tokenEnd = exeLen; } @@ -1481,9 +1481,9 @@ static bool LinuxProcessTable_recurseProcTree(LinuxProcessTable* this, openat_ar { char* endptr; unsigned long parsedPid = strtoul(name, &endptr, 10); - if (parsedPid == 0 || parsedPid == ULONG_MAX || *endptr != '\0') + if (parsedPid == 0 || parsedPid >= INT_MAX || *endptr != '\0') continue; - pid = parsedPid; + pid = (int)parsedPid; } // Skip task directory of main thread diff --git a/linux/Platform.c b/linux/Platform.c index c59be6840..4f6d2a3b2 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -507,10 +507,11 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { continue; errno = 0; - char* end = de->d_name; - int file = strtoull(de->d_name, &end, 10); - if (errno || *end) + char* end; + unsigned long res = strtoul(de->d_name, &end, 10); + if (errno || *end || res >= INT_MAX) continue; + int file = (int)res; int fd = openat(dfd, de->d_name, O_RDONLY | O_CLOEXEC); if (fd == -1) diff --git a/linux/SystemdMeter.c b/linux/SystemdMeter.c index a8df75e9e..2a1ba88b8 100644 --- a/linux/SystemdMeter.c +++ b/linux/SystemdMeter.c @@ -275,13 +275,13 @@ static void updateViaExec(bool user) { } free_and_xStrdup(&ctx->systemState, lineBuffer + strlen("SystemState=")); } else if (String_startsWith(lineBuffer, "NFailedUnits=")) { - ctx->nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10); + ctx->nFailedUnits = (unsigned int)strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10); } else if (String_startsWith(lineBuffer, "NNames=")) { - ctx->nNames = strtoul(lineBuffer + strlen("NNames="), NULL, 10); + ctx->nNames = (unsigned int)strtoul(lineBuffer + strlen("NNames="), NULL, 10); } else if (String_startsWith(lineBuffer, "NJobs=")) { - ctx->nJobs = strtoul(lineBuffer + strlen("NJobs="), NULL, 10); + ctx->nJobs = (unsigned int)strtoul(lineBuffer + strlen("NJobs="), NULL, 10); } else if (String_startsWith(lineBuffer, "NInstalledJobs=")) { - ctx->nInstalledJobs = strtoul(lineBuffer + strlen("NInstalledJobs="), NULL, 10); + ctx->nInstalledJobs = (unsigned int)strtoul(lineBuffer + strlen("NInstalledJobs="), NULL, 10); } }