fix: Optimization function deal_format nesting too deep

Signed-off-by: baoguotao <baoguotao@huawei.com>
This commit is contained in:
baoguotao 2022-04-11 09:05:13 +08:00
parent f37626e0ca
commit c0c1664312

View File

@ -20,10 +20,10 @@
#include "securec.h"
#define NUMBER 2
static const char *g_property[NUMBER] = {"%{private}", "%{public}"};
static size_t g_property_len[NUMBER] = {10, 9};
static const char *g_property[NUMBER] = { "%{private}", "%{public}" };
static size_t g_property_len[NUMBER] = { 10, 9 };
/* remove "{private}" and "{public}" from fmt and copy to dest */
/* remove "{private}" and "{public}" from fmt and copy the rest to dest */
bool deal_format(const char *fmt, char *dest, size_t size)
{
const char *ptr = fmt;
@ -38,26 +38,27 @@ bool deal_format(const char *fmt, char *dest, size_t size)
}
while (ptr_cur != NULL && *ptr_cur != '\0') {
if (*ptr_cur == '%') {
for (i = 0; i < NUMBER; i++) {
if (strncmp(ptr_cur, g_property[i], g_property_len[i]) == 0) {
/* add 1 is for to copy char '%' */
ret = strncpy_s(&dest[index], size - index, ptr, ptr_cur - ptr + 1);
if (ret != EOK) {
printk("%s strncpy_s error %d", __func__, ret);
return false;
}
index += (ptr_cur - ptr + 1);
ptr = ptr_cur + g_property_len[i];
ptr_cur = ptr;
break;
}
}
if (i == NUMBER)
ptr_cur++;
} else {
if (*ptr_cur != '%') {
ptr_cur++;
continue;
}
for (i = 0; i < NUMBER; i++) {
if (strncmp(ptr_cur, g_property[i], g_property_len[i]) != 0)
continue;
/* add 1 is for to copy char '%' */
ret = strncpy_s(&dest[index], size - index, ptr, ptr_cur - ptr + 1);
if (ret != EOK) {
printk("%s strncpy_s error %d", __func__, ret);
return false;
}
index += (ptr_cur - ptr + 1);
ptr = ptr_cur + g_property_len[i];
ptr_cur = ptr;
break;
}
if (i == NUMBER)
ptr_cur++;
}
ret = strcat_s(&dest[index], size - index, ptr);
if (ret != EOK) {
@ -66,4 +67,4 @@ bool deal_format(const char *fmt, char *dest, size_t size)
}
return true;
}
EXPORT_SYMBOL(deal_format);
EXPORT_SYMBOL(deal_format);