Skip to content

Commit cbaf3e7

Browse files
ZideChen0wenlingz
authored andcommitted
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>
1 parent d661d44 commit cbaf3e7

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

devicemodel/core/vmmapi.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,46 +116,47 @@ int acrn_parse_cpu_affinity(char *opt)
116116
return -1;
117117
}
118118

119-
while (cp) {
119+
/* white spaces within the commane line are invalid */
120+
while (cp && isdigit(cp[0])) {
120121
str = strpbrk(cp, ",-");
121122

122123
/* no more entries delimited by ',' or '-' */
123124
if (!str) {
124125
if (!dm_strtoi(cp, NULL, 10, &pcpu_id)) {
125126
add_one_pcpu(pcpu_id);
126-
break;
127127
}
128-
}
129-
130-
if (*str == ',') {
131-
/* after this, 'cp' points to the character after ',' */
132-
str = strsep(&cp, ",");
133-
134-
/* parse the entry before ',' */
135-
if (dm_strtoi(str, NULL, 10, &pcpu_id)) {
136-
return -1;
128+
break;
129+
} else {
130+
if (*str == ',') {
131+
/* after this, 'cp' points to the character after ',' */
132+
str = strsep(&cp, ",");
133+
134+
/* parse the entry before ',' */
135+
if (dm_strtoi(str, NULL, 10, &pcpu_id)) {
136+
return -1;
137+
}
138+
add_one_pcpu(pcpu_id);
137139
}
138-
add_one_pcpu(pcpu_id);
139-
}
140140

141-
if (*str == '-') {
142-
str = strsep(&cp, "-");
141+
if (*str == '-') {
142+
str = strsep(&cp, "-");
143143

144-
/* parse the entry before and after '-' respectively */
145-
if (dm_strtoi(str, NULL, 10, &pcpu_start) || dm_strtoi(cp, NULL, 10, &pcpu_end)) {
146-
return -1;
147-
}
144+
/* parse the entry before and after '-' respectively */
145+
if (dm_strtoi(str, NULL, 10, &pcpu_start) || dm_strtoi(cp, NULL, 10, &pcpu_end)) {
146+
return -1;
147+
}
148148

149-
if (pcpu_end <= pcpu_start) {
150-
return -1;
151-
}
149+
if (pcpu_end <= pcpu_start) {
150+
return -1;
151+
}
152152

153-
for (; pcpu_start <= pcpu_end; pcpu_start++) {
154-
add_one_pcpu(pcpu_start);
155-
}
153+
for (; pcpu_start <= pcpu_end; pcpu_start++) {
154+
add_one_pcpu(pcpu_start);
155+
}
156156

157-
/* skip the ',' after pcpu_end */
158-
str = strsep(&cp, ",");
157+
/* skip the ',' after pcpu_end */
158+
str = strsep(&cp, ",");
159+
}
159160
}
160161
}
161162

0 commit comments

Comments
 (0)