Fix segmentation fault when column name is NULL.

So, some columns (ex: SECATTR) can be sortable now.
This commit is contained in:
Erdem Ersoy 2020-10-30 19:12:17 +03:00 committed by Christian Göttsche
parent 6787c43097
commit 59ef15b2ad
3 changed files with 9 additions and 12 deletions

View File

@ -135,15 +135,6 @@ ColumnsPanel* ColumnsPanel_new(Settings* settings) {
return this; return this;
} }
int ColumnsPanel_fieldNameToIndex(const char* name) {
for (int j = 1; j <= Platform_numberOfFields; j++) {
if (String_eq(name, Process_fields[j].name)) {
return j;
}
}
return -1;
}
void ColumnsPanel_update(Panel* super) { void ColumnsPanel_update(Panel* super) {
ColumnsPanel* this = (ColumnsPanel*) super; ColumnsPanel* this = (ColumnsPanel*) super;
int size = Panel_size(super); int size = Panel_size(super);

View File

@ -23,8 +23,6 @@ extern const PanelClass ColumnsPanel_class;
ColumnsPanel* ColumnsPanel_new(Settings* settings); ColumnsPanel* ColumnsPanel_new(Settings* settings);
int ColumnsPanel_fieldNameToIndex(const char* name);
void ColumnsPanel_update(Panel* super); void ColumnsPanel_update(Panel* super);
#endif #endif

10
htop.c
View File

@ -129,7 +129,15 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
} }
exit(0); exit(0);
} }
flags.sortKey = ColumnsPanel_fieldNameToIndex(optarg); flags.sortKey = -1;
for (int j = 1; j < Platform_numberOfFields; j++) {
if (Process_fields[j].name == NULL)
continue;
if (String_eq(optarg, Process_fields[j].name)) {
flags.sortKey = j;
break;
}
}
if (flags.sortKey == -1) { if (flags.sortKey == -1) {
fprintf(stderr, "Error: invalid column \"%s\".\n", optarg); fprintf(stderr, "Error: invalid column \"%s\".\n", optarg);
exit(1); exit(1);