hv: refine strncpy_s to only one exit point
Fix procedure has more than one exit point
Acked-by: Eddie Dong
@@ -203,28 +203,28 @@ char *strncpy_s(char *d_arg, size_t dmax, const char *s_arg, size_t slen_arg)
{
const char *s = s_arg;
char *d = d_arg;
- char *dest_base;
+ char *pret;
size_t dest_avail;
uint64_t overlap_guard;
size_t slen = slen_arg;
if ((d == NULL) || (s == NULL)) {
pr_err("%s: invlaid src or dest buffer", __func__);
- return NULL;
+ pret = NULL;
+ } else {
+ pret = d_arg;
}
+ if (pret != NULL) {
if ((dmax == 0U) || (slen == 0U)) {
pr_err("%s: invlaid length of src or dest buffer", __func__);
- return NULL;
+ pret = NULL;
}
-
- if (d == s) {
- return d;
}
+ /* if d equal to s, just return d; else execute the below code */
+ if ((pret != NULL) && (d != s)) {
overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1));
-
- dest_base = d;
dest_avail = dmax;
while (dest_avail > 0U) {
@@ -232,17 +232,21 @@ char *strncpy_s(char *d_arg, size_t dmax, const char *s_arg, size_t slen_arg)
pr_err("%s: overlap happened.", __func__);
d--;
*d = '\0';
- return NULL;
+ /* break out to return */
+ pret = NULL;
+ break;
}
if (slen == 0U) {
*d = '\0';
- return dest_base;
+ /* break out to return */
+ break;
}
*d = *s;
if (*d == '\0') {
- return dest_base;
+ /* break out to return */
+ break;
}
d++;
@@ -252,14 +256,15 @@ char *strncpy_s(char *d_arg, size_t dmax, const char *s_arg, size_t slen_arg)
overlap_guard--;
}
+ if (dest_avail == 0U) {
pr_err("%s: dest buffer has no enough space.", __func__);
- /*
- * to avoid a string that is not
- * null-terminated in dest buffer
- */
- dest_base[dmax - 1] = '\0';
- return NULL;
+ /* to avoid a string that is not null-terminated in dest buffer */
+ pret[dmax - 1] = '\0';
+ }
+ }
+
+ return pret;
}
/**
