mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
fix(yutil): strlist_sorted_add should not leave a dangling list
realloc() may move the array before the duplicate check returns early, leaving *strlist pointing at freed memory. Assign the new pointer before inspecting entries. Refs GHSA-v42w-m2h7-fm23.
This commit is contained in:
@@ -72,6 +72,21 @@ static void check_class_selector_over_limit(void)
|
||||
ctest_equal_int("should survive overlong class selector", count, 1);
|
||||
}
|
||||
|
||||
static void check_duplicate_class_selector(void)
|
||||
{
|
||||
char css[560];
|
||||
int i;
|
||||
int count;
|
||||
|
||||
for (i = 0; i < 250; ++i) {
|
||||
css[i * 2] = '.';
|
||||
css[i * 2 + 1] = 'a';
|
||||
}
|
||||
strcpy(css + 500, "{display:block;}");
|
||||
count = parse_chunked_css("parser-security/duplicate", css);
|
||||
ctest_equal_int("should reject duplicate class selector", count, 0);
|
||||
}
|
||||
|
||||
void test_css_parser_security(void)
|
||||
{
|
||||
css_init();
|
||||
@@ -81,4 +96,6 @@ void test_css_parser_security(void)
|
||||
ctest_describe("selector name at limit", check_selector_name_at_limit);
|
||||
ctest_describe("class selector over limit",
|
||||
check_class_selector_over_limit);
|
||||
ctest_describe("duplicate class selector",
|
||||
check_duplicate_class_selector);
|
||||
}
|
||||
|
||||
+165
-164
@@ -44,206 +44,207 @@
|
||||
static strpool_t *pool = NULL;
|
||||
|
||||
static const char *parse_next_token(const char *str, char *token,
|
||||
size_t token_max_len)
|
||||
size_t token_max_len)
|
||||
{
|
||||
size_t i;
|
||||
const char *p = str;
|
||||
size_t i;
|
||||
const char *p = str;
|
||||
|
||||
for (; *p == ' '; ++p);
|
||||
for (i = 0; i < token_max_len && *p && *p != ' '; ++i, ++p) {
|
||||
token[i] = *p;
|
||||
}
|
||||
token[i] = 0;
|
||||
return p;
|
||||
for (; *p == ' '; ++p);
|
||||
for (i = 0; i < token_max_len && *p && *p != ' '; ++i, ++p) {
|
||||
token[i] = *p;
|
||||
}
|
||||
token[i] = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
int strlist_sorted_add(strlist_t *strlist, const char *str)
|
||||
{
|
||||
int i, pos, n;
|
||||
strlist_t newlist;
|
||||
int i, pos, n;
|
||||
strlist_t newlist;
|
||||
|
||||
if (*strlist) {
|
||||
for (i = 0; (*strlist)[i]; ++i);
|
||||
n = i + 2;
|
||||
} else {
|
||||
n = 2;
|
||||
}
|
||||
newlist = (strlist_t)realloc(*strlist, sizeof(char *) * n);
|
||||
if (!newlist) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
newlist[n - 2] = NULL;
|
||||
for (i = 0, pos = -1; newlist[i]; ++i) {
|
||||
int tmp = strcmp(newlist[i], str);
|
||||
if (tmp < 0) {
|
||||
continue;
|
||||
} else if (tmp == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!pool) {
|
||||
pool = strpool_create();
|
||||
}
|
||||
if (pos >= 0) {
|
||||
for (i = n - 2; i > pos; --i) {
|
||||
newlist[i] = newlist[i - 1];
|
||||
}
|
||||
} else {
|
||||
pos = n - 2;
|
||||
}
|
||||
newlist[pos] = strpool_alloc_str(pool, str);
|
||||
newlist[n - 1] = NULL;
|
||||
*strlist = newlist;
|
||||
return 0;
|
||||
if (*strlist) {
|
||||
for (i = 0; (*strlist)[i]; ++i);
|
||||
n = i + 2;
|
||||
} else {
|
||||
n = 2;
|
||||
}
|
||||
newlist = (strlist_t)realloc(*strlist, sizeof(char *) * n);
|
||||
if (!newlist) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
*strlist = newlist;
|
||||
newlist[n - 2] = NULL;
|
||||
for (i = 0, pos = -1; newlist[i]; ++i) {
|
||||
int tmp = strcmp(newlist[i], str);
|
||||
if (tmp < 0) {
|
||||
continue;
|
||||
} else if (tmp == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!pool) {
|
||||
pool = strpool_create();
|
||||
}
|
||||
if (pos >= 0) {
|
||||
for (i = n - 2; i > pos; --i) {
|
||||
newlist[i] = newlist[i - 1];
|
||||
}
|
||||
} else {
|
||||
pos = n - 2;
|
||||
}
|
||||
newlist[pos] = strpool_alloc_str(pool, str);
|
||||
newlist[n - 1] = NULL;
|
||||
*strlist = newlist;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int strlist_add_one(strlist_t *strlist, const char *str)
|
||||
{
|
||||
int i = 0;
|
||||
char **newlist;
|
||||
int i = 0;
|
||||
char **newlist;
|
||||
|
||||
if (!*strlist) {
|
||||
newlist = (char **)malloc(sizeof(char *) * 2);
|
||||
goto check_done;
|
||||
}
|
||||
for (i = 0; (*strlist)[i]; ++i) {
|
||||
if (strcmp((*strlist)[i], str) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
newlist = (char **)realloc(*strlist, (i + 2) * sizeof(char *));
|
||||
if (!*strlist) {
|
||||
newlist = (char **)malloc(sizeof(char *) * 2);
|
||||
goto check_done;
|
||||
}
|
||||
for (i = 0; (*strlist)[i]; ++i) {
|
||||
if (strcmp((*strlist)[i], str) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
newlist = (char **)realloc(*strlist, (i + 2) * sizeof(char *));
|
||||
|
||||
check_done:
|
||||
if (!newlist) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (!pool) {
|
||||
pool = strpool_create();
|
||||
}
|
||||
newlist[i] = strpool_alloc_str(pool, str);
|
||||
newlist[i + 1] = NULL;
|
||||
*strlist = newlist;
|
||||
return 0;
|
||||
if (!newlist) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (!pool) {
|
||||
pool = strpool_create();
|
||||
}
|
||||
newlist[i] = strpool_alloc_str(pool, str);
|
||||
newlist[i + 1] = NULL;
|
||||
*strlist = newlist;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int strlist_add(strlist_t *strlist, const char *str)
|
||||
{
|
||||
char buff[STRLIST_TOKEN_MAX_LEN];
|
||||
int count = 0, i, head;
|
||||
char buff[STRLIST_TOKEN_MAX_LEN];
|
||||
int count = 0, i, head;
|
||||
|
||||
for (head = 0, i = 0; str[i]; ++i) {
|
||||
if (str[i] != ' ') {
|
||||
continue;
|
||||
}
|
||||
if (i > head) {
|
||||
strncpy(buff, &str[head], i - head);
|
||||
buff[i - head] = 0;
|
||||
if (strlist_add_one(strlist, buff) == 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
head = i + 1;
|
||||
}
|
||||
if (i > head) {
|
||||
strncpy(buff, &str[head], i - head);
|
||||
buff[i - head] = 0;
|
||||
if (strlist_add_one(strlist, buff) == 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
for (head = 0, i = 0; str[i]; ++i) {
|
||||
if (str[i] != ' ') {
|
||||
continue;
|
||||
}
|
||||
if (i > head) {
|
||||
strncpy(buff, &str[head], i - head);
|
||||
buff[i - head] = 0;
|
||||
if (strlist_add_one(strlist, buff) == 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
head = i + 1;
|
||||
}
|
||||
if (i > head) {
|
||||
strncpy(buff, &str[head], i - head);
|
||||
buff[i - head] = 0;
|
||||
if (strlist_add_one(strlist, buff) == 0) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int strlist_has(strlist_t strlist, const char *str)
|
||||
{
|
||||
int i;
|
||||
const char *p = str;
|
||||
char token[STRLIST_TOKEN_MAX_LEN];
|
||||
int i;
|
||||
const char *p = str;
|
||||
char token[STRLIST_TOKEN_MAX_LEN];
|
||||
|
||||
if (!strlist) {
|
||||
return 0;
|
||||
}
|
||||
while (*p) {
|
||||
p = parse_next_token(p, token, STRLIST_TOKEN_MAX_LEN - 1);
|
||||
for (i = 0; strlist[i]; ++i) {
|
||||
if (strcmp(strlist[i], token) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
if (!strlist) {
|
||||
return 0;
|
||||
}
|
||||
while (*p) {
|
||||
p = parse_next_token(p, token, STRLIST_TOKEN_MAX_LEN - 1);
|
||||
for (i = 0; strlist[i]; ++i) {
|
||||
if (strcmp(strlist[i], token) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int strlist_remove_one(strlist_t *strlist, const char *str)
|
||||
{
|
||||
int i, pos, len;
|
||||
strlist_t newlist;
|
||||
int i, pos, len;
|
||||
strlist_t newlist;
|
||||
|
||||
if (!*strlist) {
|
||||
return 0;
|
||||
}
|
||||
for (len = 0; (*strlist)[len]; ++len);
|
||||
for (pos = -1, i = 0; i < len; ++i) {
|
||||
if (strcmp((*strlist)[i], str) == 0) {
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos == -1) {
|
||||
return 0;
|
||||
}
|
||||
strpool_free_str((*strlist)[pos]);
|
||||
if (pos == 0 && len < 2) {
|
||||
free(*strlist);
|
||||
*strlist = NULL;
|
||||
return 1;
|
||||
}
|
||||
newlist = (char **)malloc(len * sizeof(char *));
|
||||
for (i = 0; i < pos; ++i) {
|
||||
newlist[i] = (*strlist)[i];
|
||||
}
|
||||
for (i = pos; i < len; ++i) {
|
||||
newlist[i] = (*strlist)[i + 1];
|
||||
}
|
||||
newlist[len - 1] = NULL;
|
||||
free(*strlist);
|
||||
*strlist = newlist;
|
||||
return 1;
|
||||
if (!*strlist) {
|
||||
return 0;
|
||||
}
|
||||
for (len = 0; (*strlist)[len]; ++len);
|
||||
for (pos = -1, i = 0; i < len; ++i) {
|
||||
if (strcmp((*strlist)[i], str) == 0) {
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos == -1) {
|
||||
return 0;
|
||||
}
|
||||
strpool_free_str((*strlist)[pos]);
|
||||
if (pos == 0 && len < 2) {
|
||||
free(*strlist);
|
||||
*strlist = NULL;
|
||||
return 1;
|
||||
}
|
||||
newlist = (char **)malloc(len * sizeof(char *));
|
||||
for (i = 0; i < pos; ++i) {
|
||||
newlist[i] = (*strlist)[i];
|
||||
}
|
||||
for (i = pos; i < len; ++i) {
|
||||
newlist[i] = (*strlist)[i + 1];
|
||||
}
|
||||
newlist[len - 1] = NULL;
|
||||
free(*strlist);
|
||||
*strlist = newlist;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int strlist_remove(strlist_t *strlist, const char *str)
|
||||
{
|
||||
char token[STRLIST_TOKEN_MAX_LEN];
|
||||
int count = 0, i, head;
|
||||
char token[STRLIST_TOKEN_MAX_LEN];
|
||||
int count = 0, i, head;
|
||||
|
||||
for (head = 0, i = 0; str[i]; ++i) {
|
||||
if (str[i] != ' ') {
|
||||
continue;
|
||||
}
|
||||
if (i - 1 > head) {
|
||||
strncpy(token, &str[head], i - head);
|
||||
token[i - head] = 0;
|
||||
count += strlist_remove_one(strlist, token);
|
||||
}
|
||||
head = i + 1;
|
||||
}
|
||||
if (i - 1 > head) {
|
||||
strncpy(token, &str[head], i - head);
|
||||
token[i - head] = 0;
|
||||
count += strlist_remove_one(strlist, token);
|
||||
}
|
||||
return count;
|
||||
for (head = 0, i = 0; str[i]; ++i) {
|
||||
if (str[i] != ' ') {
|
||||
continue;
|
||||
}
|
||||
if (i - 1 > head) {
|
||||
strncpy(token, &str[head], i - head);
|
||||
token[i - head] = 0;
|
||||
count += strlist_remove_one(strlist, token);
|
||||
}
|
||||
head = i + 1;
|
||||
}
|
||||
if (i - 1 > head) {
|
||||
strncpy(token, &str[head], i - head);
|
||||
token[i - head] = 0;
|
||||
count += strlist_remove_one(strlist, token);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void strlist_free(strlist_t strlist)
|
||||
{
|
||||
int i = 0;
|
||||
while (strlist[i]) {
|
||||
strpool_free_str(strlist[i]);
|
||||
++i;
|
||||
}
|
||||
free(strlist);
|
||||
int i = 0;
|
||||
while (strlist[i]) {
|
||||
strpool_free_str(strlist[i]);
|
||||
++i;
|
||||
}
|
||||
free(strlist);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user