tools/power turbostat: pmt: Improve sscanf() hygiene

Explicitly check sscanf() return values for number of matches,
and handle EOF (-1).

Defensive programming -- not an issue actively seen by users.

Fixes: 4265a86582 ("tools/power turbostat: Add PMT directory iterator helper")
Assisted-by: Claude:claude-sonnet-4.6
Signed-off-by: Len Brown <len.brown@intel.com>
This commit is contained in:
Len Brown 2026-05-04 15:38:34 -04:00
parent e1708e514a
commit e817a533bb

View File

@ -2011,15 +2011,17 @@ int pmt_telemdir_filter(const struct dirent *e)
{
unsigned int dummy;
return sscanf(e->d_name, "telem%u", &dummy);
return (sscanf(e->d_name, "telem%u", &dummy) == 1);
}
int pmt_telemdir_sort(const struct dirent **a, const struct dirent **b)
{
unsigned int aidx = 0, bidx = 0;
sscanf((*a)->d_name, "telem%u", &aidx);
sscanf((*b)->d_name, "telem%u", &bidx);
if (sscanf((*a)->d_name, "telem%u", &aidx) != 1)
aidx = 0;
if (sscanf((*b)->d_name, "telem%u", &bidx) != 1)
bidx = 0;
return (aidx > bidx) ? 1 : (aidx < bidx) ? -1 : 0;
}