hv: refine strnlen_s/strstr_s to only one exit point
Fix procedure has more than one exit point
@@ -288,14 +288,10 @@ char *strncpy_s(char *d_arg, size_t dmax, const char *s_arg, size_t slen_arg)
size_t strnlen_s(const char *str_arg, size_t maxlen_arg)
{
const char *str = str_arg;
- size_t maxlen = maxlen_arg;
- size_t count;
-
- if (str == NULL) {
- return 0;
- }
+ size_t count = 0U;
- count = 0U;
+ if (str != NULL) {
+ size_t maxlen = maxlen_arg;
while ((*str) != '\0') {
if (maxlen == 0U) {
break;
@@ -305,6 +301,7 @@ size_t strnlen_s(const char *str_arg, size_t maxlen_arg)
maxlen--;
str++;
}
+ }
return count;
}
@@ -359,28 +356,23 @@ char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen
{
size_t len1, len2;
size_t i;
- const char *pstr;
+ const char *pstr, *pret;
if ((str1 == NULL) || (str2 == NULL)) {
- return NULL;
- }
-
- if ((maxlen1 == 0U) || (maxlen2 == 0U)) {
- return NULL;
- }
-
+ pret = NULL;
+ } else if ((maxlen1 == 0U) || (maxlen2 == 0U)) {
+ pret = NULL;
+ } else {
len1 = strnlen_s(str1, maxlen1);
len2 = strnlen_s(str2, maxlen2);
if (len1 < len2) {
- return NULL;
- }
-
+ pret = NULL;
+ } else if ((str1 == str2) || (len2 == 0U)) {
/* return str1 if str2 equals to str1 or str2 points to a string with zero length*/
- if ((str1 == str2) || (len2 == 0U)) {
- return (char *)str1;
- }
-
+ pret = str1;
+ } else {
+ pret = NULL;
pstr = str1;
while (len1 >= len2) {
for (i = 0U; i < len2; i++) {
@@ -389,14 +381,14 @@ char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen
}
}
if (i == len2) {
- return (char *)pstr;
+ pret = pstr;
+ break;
}
pstr++;
len1--;
}
+ }
+ }
- /*
- * substring was not found, return NULL
- */
- return NULL;
+ return (char *)pret;
}
