acrn-dm: fix corner cases in acrn_parse_cpu_affinity()

- re-arange the code to make static code analysis tool happy.

- If no valid conversion could be performed, a zero value is returned
  (0L) from strtol(), so add a sanity check "isdigit(cp[0])" to ensure
  that it won't unexpectedly parse CPU 0 if the string starts or ends
  with the valid delimiters ',' or '-', for example:

  -- cpu_affinity 1,
  -- cpu_affinity ,1

Tracked-On: #4616
Signed-off-by: Zide Chen <zide.chen@intel.com>
This commit is contained in:
Zide Chen 2020-04-27 11:46:06 -07:00 committed by wenlingz
parent 0f6d11866b
commit f6a7206200
1 changed files with 29 additions and 28 deletions

View File

@ -132,17 +132,17 @@ int acrn_parse_cpu_affinity(char *opt)
return -1; return -1;
} }
while (cp) { /* white spaces within the commane line are invalid */
while (cp && isdigit(cp[0])) {
str = strpbrk(cp, ",-"); str = strpbrk(cp, ",-");
/* no more entries delimited by ',' or '-' */ /* no more entries delimited by ',' or '-' */
if (!str) { if (!str) {
if (!dm_strtoi(cp, NULL, 10, &pcpu_id)) { if (!dm_strtoi(cp, NULL, 10, &pcpu_id)) {
add_one_pcpu(pcpu_id); add_one_pcpu(pcpu_id);
}
break; break;
} } else {
}
if (*str == ',') { if (*str == ',') {
/* after this, 'cp' points to the character after ',' */ /* after this, 'cp' points to the character after ',' */
str = strsep(&cp, ","); str = strsep(&cp, ",");
@ -174,6 +174,7 @@ int acrn_parse_cpu_affinity(char *opt)
str = strsep(&cp, ","); str = strsep(&cp, ",");
} }
} }
}
return 0; return 0;
} }