Skip to content

Commit b195149

Browse files
fuyanXwenlingz
authored andcommitted
acrn-hv: code review fix lib/string.c
In lib/string.c, strncmp doesn't consider condition "n_arg=0", just add a process to "n_arg=0". Tracked-On: #4093 Tracked-On: #3466 Signed-off-by: YanX Fu <yanx.fu@intel.com> Reviewed-by: Yonghua Huang <yonghua.huang@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 6730660 commit b195149

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

hypervisor/lib/string.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,26 @@ int32_t strcmp(const char *s1_arg, const char *s2_arg)
232232
return *str1 - *str2;
233233
}
234234

235+
/**
236+
* @pre n_arg > 0
237+
*/
235238
int32_t strncmp(const char *s1_arg, const char *s2_arg, size_t n_arg)
236239
{
237240
const char *str1 = s1_arg;
238241
const char *str2 = s2_arg;
239242
size_t n = n_arg;
243+
int32_t ret = 0;
240244

241-
while (((n - 1) != 0U) && ((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
242-
str1++;
243-
str2++;
244-
n--;
245+
if (n > 0U) {
246+
while (((n - 1) != 0U) && ((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
247+
str1++;
248+
str2++;
249+
n--;
250+
}
251+
ret = (int32_t) (*str1 - *str2);
245252
}
246253

247-
return *str1 - *str2;
254+
return ret;
248255
}
249256

250257
/*

0 commit comments

Comments
 (0)