Add array bounds checking for the Process_fields array (covscan)

Coverity scan reports there may be a code path that would cause
an overrun in the (relatively new) ScreenSettings code where it
evaluates default sort direction.  Add bounds check and default
to descending instead of a potentially invalid array access.
This commit is contained in:
Nathan Scott 2022-04-30 13:50:25 +10:00
parent ae518e20b7
commit cb61865bb9
1 changed files with 7 additions and 1 deletions

View File

@ -271,12 +271,18 @@ static void ScreenSettings_readFields(ScreenSettings* ss, Hashtable* columns, co
ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults) {
int sortKey = defaults->sortKey ? toFieldIndex(this->dynamicColumns, defaults->sortKey) : PID;
int sortDesc;
if (sortKey >= 0 && sortKey < LAST_PROCESSFIELD)
sortDesc = Process_fields[sortKey].defaultSortDesc;
else
sortDesc = 1;
ScreenSettings* ss = xMalloc(sizeof(ScreenSettings));
*ss = (ScreenSettings) {
.name = xStrdup(defaults->name),
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(ProcessField)),
.flags = 0,
.direction = (Process_fields[sortKey].defaultSortDesc) ? -1 : 1,
.direction = sortDesc ? -1 : 1,
.treeDirection = 1,
.sortKey = sortKey,
.treeSortKey = PID,