Skip to content

Commit

Permalink
Fixed infinite loop and added unit tests
Browse files Browse the repository at this point in the history
Fixes issue #165
  • Loading branch information
jstedfast committed Jun 11, 2024
1 parent 4100b76 commit 83cb62c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gmime/gmime-param.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ g_mime_param_list_encode (GMimeParamList *list, GMimeFormatOptions *options, gbo

if (toolong && method == GMIME_PARAM_ENCODING_METHOD_RFC2231) {
/* we need to do special rfc2184 parameter wrapping */
size_t maxlen = GMIME_FOLD_LEN - (nlen + 6);
size_t maxlen = MAX (GMIME_FOLD_LEN - (nlen + 6), 3);
char *inend;
int n = 0;

Expand Down
65 changes: 65 additions & 0 deletions tests/test-headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,67 @@ test_header_formatting (void)
g_object_unref (list);
}

static struct {
const char *value;
const char *rfc2047_encoded;
const char *rfc2231_encoded;
} parameter_lists[] = {
{ "ќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќ=\"ќ\"",
";\n\tќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќ=\"=?utf-8?b?0Zw=?=\"\n",
";\n\tќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќ*0*=%D1;\n\tќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќќ*1*=%9C\n" },
};

static void
test_parameter_lists (void)
{
GMimeFormatOptions *rfc2047, *rfc2231;
GMimeParamList *list;
GString *str;
size_t len;
guint i;

str = g_string_new ("Content-Type: text/plain");
len = str->length;

rfc2047 = g_mime_format_options_new ();
g_mime_format_options_set_new_line_format (rfc2047, GMIME_NEWLINE_FORMAT_UNIX);
g_mime_format_options_set_param_encoding_method (rfc2047, GMIME_PARAM_ENCODING_METHOD_RFC2047);

rfc2231 = g_mime_format_options_clone (rfc2047);
g_mime_format_options_set_param_encoding_method (rfc2047, GMIME_PARAM_ENCODING_METHOD_RFC2231);

for (i = 0; i < G_N_ELEMENTS (parameter_lists); i++) {
testsuite_check ("parameter_lists[%u]", i);

list = g_mime_param_list_parse (NULL, parameter_lists[i].value);

try {
g_mime_param_list_encode (list, rfc2047, TRUE, str);

if (strcmp (parameter_lists[i].rfc2047_encoded, str->str + len) != 0)
throw (exception_new ("rfc2047 encoded values do not match: %s", str->str + len));

g_string_truncate (str, len);
g_mime_param_list_encode (list, rfc2231, TRUE, str);

if (strcmp (parameter_lists[i].rfc2231_encoded, str->str + len) != 0)
throw (exception_new ("rfc2231 encoded values do not match: %s", str->str + len));

testsuite_check_passed ();
} catch (ex) {
testsuite_check_failed ("parameter_lists[%u] failed: %s", i, ex->message);
g_object_unref (list);
} finally;

g_string_truncate (str, len);
g_object_unref (list);
}

g_object_unref (rfc2047);
g_object_unref (rfc2231);
g_string_free (str, TRUE);
}

int main (int argc, char **argv)
{
g_mime_init ();
Expand Down Expand Up @@ -624,6 +685,10 @@ int main (int argc, char **argv)
testsuite_start ("header formatting");
test_header_formatting ();
testsuite_end ();

testsuite_start ("parameter lists");
test_parameter_lists ();
testsuite_end ();

g_mime_shutdown ();

Expand Down

0 comments on commit 83cb62c

Please sign in to comment.