Skip to content

Commit

Permalink
Merge pull request #10 from Lihis/use-len-in-irc_message_parse
Browse files Browse the repository at this point in the history
Respect "len" parameter in irc_message_parse()
  • Loading branch information
n0la authored Sep 21, 2023
2 parents b8979a1 + 1219271 commit adad148
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/irc.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ static irc_error_t irc_think_data(irc_t i)
/* strip \r\n from the line
*/
if (linesize > 2) {
line[linesize-2] = '\0';
linesize -= 2;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ irc_error_t irc_message_parse(irc_message_t c, char const *l, size_t len)
irc_tag_t *tags = NULL;
size_t tagslen = 0;

char *line = strdup(l);
char *line = len < 0 ? strdup(l) : strndup(l, len);
char *ptr = line;
char *part = NULL;
irc_error_t r = irc_error_memory;
Expand Down
14 changes: 5 additions & 9 deletions tests/test_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void test_message_parse_without_tag(void **data)
const char *str = ":prefix COMMAND Arg";
irc_error_t error = irc_error_internal;

error = irc_message_parse(m, str, strlen(str));
error = irc_message_parse(m, str, -1);
assert_int_equal(error, irc_error_success);
assert_string_equal(m->prefix, "prefix");
assert_string_equal(m->command, "COMMAND");
Expand All @@ -30,7 +30,7 @@ static void test_message_parse_with_single_tag(void **data)
const char *str = "@time=0000 :prefix COMMAND Arg";
irc_error_t error = irc_error_internal;

error = irc_message_parse(m, str, strlen(str));
error = irc_message_parse(m, str, -1);
assert_int_equal(error, irc_error_success);
assert_string_equal(m->prefix, "prefix");
assert_string_equal(m->command, "COMMAND");
Expand All @@ -49,7 +49,7 @@ static void test_message_parse_with_multiple_tag(void **data)
const char *str = "@key1=value1;key2;key3=value3 :prefix COMMAND Arg";
irc_error_t error = irc_error_internal;

error = irc_message_parse(m, str, strlen(str));
error = irc_message_parse(m, str, -1);
assert_int_equal(error, irc_error_success);
assert_string_equal(m->prefix, "prefix");
assert_string_equal(m->command, "COMMAND");
Expand All @@ -70,22 +70,18 @@ static void test_message_string(void **data)
{
irc_message_t m = irc_message_new();
const char *expected = "@key1=value1;key2=value2 :prefix COMMAND Arg\r\n";
char *str = NULL;
irc_error_t error = irc_error_internal;
char *actual = NULL;
size_t len = 0;

// Allocate string for parsing without the "\r\n"
str = calloc(sizeof(char), strlen(expected) - 1);
memcpy(str, expected, strlen(expected) - 2);
error = irc_message_parse(m, str, strlen(str));
// Pass length minus the "\r\n"
error = irc_message_parse(m, expected, strlen(expected) - 2);
assert_return_code(error, irc_error_success);

error = irc_message_string(m, &actual, &len);
assert_return_code(error, irc_error_success);
assert_string_equal(actual, expected);

free(str);
free(actual);
irc_message_unref(m);
}
Expand Down

0 comments on commit adad148

Please sign in to comment.