Skip to content

Commit fbd42e4

Browse files
cgzonesBenBE
authored andcommitted
Silence -Wshorten-64-to-32 warnings
Enabled in LLVM 19
1 parent b1b804f commit fbd42e4

25 files changed

+130
-106
lines changed

Action.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,19 @@ static Htop_Reaction actionPrevScreen(State* st) {
401401
}
402402

403403
Htop_Reaction Action_setScreenTab(State* st, int x) {
404+
assert(x >= 0);
405+
if (x < 0)
406+
return 0;
407+
unsigned int pos = x;
404408
Settings* settings = st->host->settings;
405-
int s = 2;
409+
unsigned int s = 2;
406410
for (unsigned int i = 0; i < settings->nScreens; i++) {
407-
if (x < s) {
411+
if (pos < s) {
408412
return 0;
409413
}
410414
const char* tab = settings->screens[i]->heading;
411-
int len = strlen(tab);
412-
if (x <= s + len + 1) {
415+
size_t len = strlen(tab);
416+
if (pos <= s + len + 1) {
413417
settings->ssIndex = i;
414418
setActiveScreen(settings, st, i);
415419
return HTOP_UPDATE_PANELHDR | HTOP_REFRESH | HTOP_REDRAW_BAR;

AffinityPanel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static MaskItem* AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, u
311311
}
312312

313313
/* "[x] " + "|- " * depth + ("- ")?(if root node) + name */
314-
unsigned width = 4 + 3 * depth + (2 * !depth) + strlen(buf);
314+
unsigned width = 4 + 3 * depth + (2 * !depth) + (unsigned)strlen(buf);
315315
if (width > this->width) {
316316
this->width = width;
317317
}
@@ -389,7 +389,7 @@ Panel* AffinityPanel_new(Machine* host, const Affinity* affinity, int* width) {
389389

390390
char number[16];
391391
xSnprintf(number, 9, "CPU %d", Settings_cpuId(host->settings, i));
392-
unsigned cpu_width = 4 + strlen(number);
392+
unsigned cpu_width = 4 + (unsigned) strlen(number);
393393
if (cpu_width > this->width) {
394394
this->width = cpu_width;
395395
}

CommandLine.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212

1313
#include <assert.h>
1414
#include <ctype.h>
15+
#include <errno.h>
1516
#include <getopt.h>
1617
#include <locale.h>
1718
#include <stdbool.h>
@@ -204,12 +205,15 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
204205
if (!username) {
205206
flags->userId = geteuid();
206207
} else if (!Action_setUserOnly(username, &(flags->userId))) {
207-
for (const char* itr = username; *itr; ++itr)
208-
if (!isdigit((unsigned char)*itr)) {
209-
fprintf(stderr, "Error: invalid user \"%s\".\n", username);
210-
return STATUS_ERROR_EXIT;
211-
}
212-
flags->userId = atol(username);
208+
char *endptr;
209+
errno = 0;
210+
unsigned long res = strtoul(username, &endptr, 10);
211+
unsigned castRes = (unsigned) res;
212+
if (*endptr != '\0' || res == ULONG_MAX || errno != 0 || castRes != res) {
213+
fprintf(stderr, "Error: invalid user \"%s\".\n", username);
214+
return STATUS_ERROR_EXIT;
215+
}
216+
flags->userId = castRes;
213217
}
214218
break;
215219
}

DynamicMeter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static void DynamicMeter_getUiName(const Meter* this, char* name, size_t length)
106106
const char* uiName = meter->caption;
107107
if (uiName) {
108108
size_t uiNameLen = strlen(uiName);
109+
assert(uiNameLen < 32);
109110
if (uiNameLen > 2 && uiName[uiNameLen - 2] == ':')
110111
uiNameLen -= 2;
111112

Header.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void Header_setLayout(Header* this, HeaderLayout hLayout) {
7777
Header_calculateHeight(this);
7878
}
7979

80-
static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, unsigned int column) {
80+
static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, size_t column) {
8181
assert(column < HeaderLayout_getColumns(this->headerLayout));
8282

8383
Vector* meters = this->columns[column];
@@ -170,7 +170,7 @@ void Header_writeBackToSettings(const Header* this) {
170170
}
171171
}
172172

173-
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column) {
173+
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, size_t column) {
174174
assert(column < HeaderLayout_getColumns(this->headerLayout));
175175

176176
Vector* meters = this->columns[column];
@@ -198,8 +198,8 @@ void Header_draw(const Header* this) {
198198
for (int y = 0; y < height; y++) {
199199
mvhline(y, 0, ' ', COLS);
200200
}
201-
const int numCols = HeaderLayout_getColumns(this->headerLayout);
202-
const int width = COLS - 2 * pad - (numCols - 1);
201+
const size_t numCols = HeaderLayout_getColumns(this->headerLayout);
202+
const size_t width = COLS - 2 * pad - (numCols - 1);
203203
int x = pad;
204204
float roundingLoss = 0.0F;
205205

@@ -221,7 +221,7 @@ void Header_draw(const Header* this) {
221221
/* Let meters in text mode expand to the right on empty neighbors;
222222
except for multi column meters. */
223223
if (meter->mode == TEXT_METERMODE && !Meter_isMultiColumn(meter)) {
224-
for (int j = 1; j < meter->columnWidthCount; j++) {
224+
for (size_t j = 1; j < meter->columnWidthCount; j++) {
225225
actualWidth++; /* separator column */
226226
actualWidth += (float)width * HeaderLayout_layouts[this->headerLayout].widths[col + j] / 100.0F;
227227
}
@@ -253,7 +253,7 @@ void Header_updateData(Header* this) {
253253
* by counting how many columns to the right are empty or contain a BlankMeter.
254254
* Returns the number of columns to span, i.e. if the direct neighbor is occupied 1.
255255
*/
256-
static int calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const unsigned int curColumn, const int curHeight) {
256+
static size_t calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const size_t curColumn, const int curHeight) {
257257
for (size_t i = curColumn + 1; i < HeaderLayout_getColumns(this->headerLayout); i++) {
258258
const Vector* meters = this->columns[i];
259259

Header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void Header_populateFromSettings(Header* this);
3535

3636
void Header_writeBackToSettings(const Header* this);
3737

38-
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column);
38+
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, size_t column);
3939

4040
void Header_reinit(Header* this);
4141

IncSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef enum {
2424

2525
typedef struct IncMode_ {
2626
char buffer[INCMODE_MAX + 1];
27-
int index;
27+
size_t index;
2828
FunctionBar* bar;
2929
bool isFilter;
3030
} IncMode;

Meter.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
5454
mvaddnstr(y, x, caption, w);
5555
attrset(CRT_colors[RESET_COLOR]);
5656

57-
int captionLen = strlen(caption);
57+
int captionLen = (int)strlen(caption);
5858
x += captionLen;
5959
w -= captionLen;
6060
if (w <= 0)
@@ -251,7 +251,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
251251
// Starting positions of graph data and terminal column
252252
if ((size_t)w > nValues / 2) {
253253
x += w - nValues / 2;
254-
w = nValues / 2;
254+
w = (int)(nValues / 2);
255255
}
256256
size_t i = nValues - (size_t)w * 2;
257257

@@ -319,7 +319,9 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
319319
attrset(CRT_colors[LED_COLOR]);
320320
const char* caption = Meter_getCaption(this);
321321
mvaddstr(yText, x, caption);
322-
int xx = x + strlen(caption);
322+
size_t capLen = strlen(caption);
323+
assert(capLen < 32);
324+
int xx = x + (int) MINIMUM(capLen, 32);
323325
int len = RichString_sizeVal(out);
324326
for (int i = 0; i < len; i++) {
325327
int c = RichString_getCharVal(out, i);

Meter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct Meter_ {
112112
unsigned int param;
113113
GraphData drawData;
114114
int h;
115-
int columnWidthCount; /**< only used internally by the Header */
115+
size_t columnWidthCount; /**< only used internally by the Header */
116116
uint8_t curItems;
117117
const int* curAttributes;
118118
char txtBuffer[METER_TXTBUFFER_LEN];

Panel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ HandlerResult Panel_selectByTyping(Panel* this, int ch) {
455455
char* buffer = this->eventHandlerState;
456456

457457
if (0 < ch && ch < 255 && isgraph((unsigned char)ch)) {
458-
int len = strlen(buffer);
458+
size_t len = strlen(buffer);
459459
if (!len) {
460460
if ('/' == ch) {
461461
ch = '\001';

0 commit comments

Comments
 (0)