Skip to content

Commit f33edc6

Browse files
lifeixwenlingz
authored andcommitted
hv: fix reference parameter to procedure is reassigned
Reassign reference parameter may lead to possibly serious errors and unmaintainability. This patch wants to fix this by avoiding do this. Tracked-On: #861 Signed-off-by: Li, Fei1 <fei1.li@intel.com>
1 parent 36be890 commit f33edc6

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

hypervisor/lib/string.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen
427427
{
428428
size_t len1, len2;
429429
size_t i;
430+
const char *pstr;
430431

431432
if ((str1 == NULL) || (str2 == NULL)) {
432433
return NULL;
@@ -439,28 +440,26 @@ char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen
439440
len1 = strnlen_s(str1, maxlen1);
440441
len2 = strnlen_s(str2, maxlen2);
441442

442-
if (len1 == 0U) {
443+
if (len1 < len2) {
443444
return NULL;
444445
}
445446

446-
/*
447-
* str2 points to a string with zero length, or
448-
* str2 equals str1, return str1
449-
*/
450-
if (len2 == 0U || str1 == str2) {
447+
/* return str1 if str2 equals to str1 or str2 points to a string with zero length*/
448+
if ((str1 == str2) || (len2 == 0U)) {
451449
return (char *)str1;
452450
}
453451

452+
pstr = str1;
454453
while (len1 >= len2) {
455454
for (i = 0U; i < len2; i++) {
456-
if (str1[i] != str2[i]) {
455+
if (pstr[i] != str2[i]) {
457456
break;
458457
}
459458
}
460459
if (i == len2) {
461-
return (char *)str1;
460+
return (char *)pstr;
462461
}
463-
str1++;
462+
pstr++;
464463
len1--;
465464
}
466465

0 commit comments

Comments
 (0)