Fix crash in EGG parser utf8 conversion

Fix use of iconv output variable so we don't try to free or realloc a
bad pointer. Also add a basic unit test to validate use of the utf8 conversion
function.

Fix EGG parser mem leak.

Also adjusts valgrind suppression rules to ignore valgrind false
positives in unrar code (again).
This commit is contained in:
Micah Snyder
2020-07-15 11:21:49 -07:00
committed by Micah Snyder (micasnyd)
parent 8bb3716be9
commit d6565c5b83
4 changed files with 31 additions and 10 deletions
+9 -7
View File
@@ -625,15 +625,16 @@ cl_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, cha
}
}
if (NULL == encoding){
if (NULL == encoding) {
cli_dbgmsg("egg_filename_to_utf8: Invalid codepage parameter passed in.\n");
goto done;
}
for (attempt = 1; attempt <= 3; attempt++) {
char* out_utf8_tmp = NULL;
char * inbuf = in;
size_t iconvRet = -1;
char* out_utf8_index = NULL;
char* out_utf8_tmp = NULL;
char* inbuf = in;
size_t iconvRet = -1;
/* Charset to UTF-8 should never exceed in_size * 6;
* We can shrink final buffer after the conversion, if needed. */
@@ -642,7 +643,7 @@ cl_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, cha
inbytesleft = in_size;
outbytesleft = out_utf8_size;
out_utf8 = cli_calloc(1, out_utf8_size + 1);
out_utf8 = out_utf8_index = cli_calloc(1, out_utf8_size + 1);
if (NULL == out_utf8) {
cli_errmsg("egg_filename_to_utf8: Failure allocating buffer for utf8 data.\n");
status = CL_EMEM;
@@ -654,9 +655,9 @@ cl_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, cha
goto done;
}
iconvRet = iconv(conv, &inbuf, &inbytesleft, &out_utf8, &outbytesleft);
iconvRet = iconv(conv, &inbuf, &inbytesleft, &out_utf8_index, &outbytesleft);
iconv_close(conv);
conv = (iconv_t) -1;
conv = (iconv_t)-1;
if ((size_t)-1 == iconvRet) {
switch (errno) {
case E2BIG:
@@ -686,6 +687,7 @@ cl_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, cha
}
out_utf8 = out_utf8_tmp;
out_utf8_size = out_utf8_size - outbytesleft;
break;
}
#else
+1
View File
@@ -260,6 +260,7 @@ CLAMAV_PRIVATE {
cli_gentemp_with_prefix;
cli_basename;
cli_realpath;
cli_codepage_to_utf8;
__cli_strcasestr;
__cli_strndup;
+21
View File
@@ -973,6 +973,26 @@ START_TEST(test_sanitize_path)
}
END_TEST
cl_error_t cli_codepage_to_utf8(char* in, size_t in_size, uint16_t codepage, char** out, size_t* out_size);
START_TEST(test_cli_codepage_to_utf8)
{
cl_error_t ret;
char *utf8 = NULL;
size_t utf8_size = 0;
ret = cli_codepage_to_utf8("\x00\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x20\x00\x77\x00\x6f\x00\x72\x00\x6c\x00\x64\x00\x21\x00\x00", 26, 1201, &utf8, &utf8_size);
ck_assert_msg(CL_SUCCESS == ret, "test_cli_codepage_to_utf8: Failed to convert CODEPAGE_UTF16_LE to UTF8: ret != SUCCESS!");
ck_assert_msg(NULL != utf8, "sanitize_path: Failed to convert CODEPAGE_UTF16_LE to UTF8: utf8 pointer is NULL!");
ck_assert_msg(0 == strcmp(utf8, "Hello world!"), "sanitize_path: '%s' doesn't match '%s'", utf8, "Hello world!");
if (NULL != utf8) {
free(utf8);
utf8 = NULL;
}
}
END_TEST
static Suite *test_cli_suite(void)
{
Suite *s = suite_create("cli");
@@ -992,6 +1012,7 @@ static Suite *test_cli_suite(void)
suite_add_tcase(s, tc_cli_assorted);
tcase_add_test(tc_cli_assorted, test_sanitize_path);
tcase_add_test(tc_cli_assorted, test_cli_codepage_to_utf8);
return s;
}
-3
View File
@@ -265,7 +265,6 @@
fun:internal_ascii_loop
fun:__gconv_transform_internal_ascii
fun:wcsrtombs
fun:wcsrtombs
...
}
{
@@ -273,7 +272,6 @@
Memcheck:Cond
fun:__wcsnlen_avx2
fun:wcsrtombs
fun:wcsrtombs
...
}
{
@@ -281,7 +279,6 @@
Memcheck:Cond
fun:__wcsnlen_sse4_1
fun:wcsrtombs
fun:wcsrtombs
...
}
{