Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,8 @@ static bool HexStringToChar(const char *hex_string, char *res)
return false;
}

char tmp[hex_len + 1];
char *tmp;
tmp = alloca(hex_len + 1);
memcpy(tmp, hex_string, hex_len);
tmp[hex_len] = '\0';

Expand Down
4 changes: 2 additions & 2 deletions libutils/string_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ char *StringSubstring(const char *source, size_t source_len, int start, int len)
start = source_len + start;
}

if (start >= end)
if (start > end)
{
return NULL;
return SafeStringDuplicate("");
}

char *result = xcalloc(end - start + 2, sizeof(char));
Expand Down
55 changes: 54 additions & 1 deletion tests/unit/string_lib_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,55 @@ static void test_substring_overshoot(void)
free(new_string);
}

static void test_substring_result_is_one_char(void)
{
{
char *new_string = StringSubstring("[1]", 3, 1, -1);
assert_string_equal(new_string, "1");
free(new_string);
}
{
char *new_string = StringSubstring("[1]", 3, 0, 1);
assert_string_equal(new_string, "[");
free(new_string);
}
{
char *new_string = StringSubstring("[1]", 3, 2, 1);
assert_string_equal(new_string, "]");
free(new_string);
}
{
char *new_string = StringSubstring("1", 1, 0, 1);
assert_string_equal(new_string, "1");
free(new_string);
}
{
char *new_string = StringSubstring("1", 1, 0, -1);
assert_string_equal(new_string, "1");
free(new_string);
}
{
char *new_string = StringSubstring("1", 1, 0, 0);
assert_string_equal(new_string, "");
free(new_string);
}
}

static void test_substring_empty_input(void)
{
char *new_string = StringSubstring("", 0, 0, 1);
assert_string_equal(new_string, "");
free(new_string);
}

Comment on lines +372 to +412

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, but let's maybe add a few more tests to be sure? I.e. what happens when the 1 char string is at the beginning or end? What happens when the input string is also 1 character? What about when the input or the result is 0 length?

static void test_substring_empty_output(void)
{
char *new_string = StringSubstring("\"\"", 2, 1, -1);
assert_string_equal(new_string, "");
free(new_string);
}


static void test_substring_positive(void)
{
char *new_string = StringSubstring("abcdef", 6, 2, 3);
Expand Down Expand Up @@ -396,8 +445,9 @@ static void test_substring_negative(void)
static void test_substring_evil(void)
{
char *new_string = StringSubstring("abcdef", 6, 4, -4);
assert_string_equal(new_string, "");
free(new_string);

assert_int_equal(new_string, NULL);
}

static void test_string_to_long(void)
Expand Down Expand Up @@ -1568,6 +1618,9 @@ int main()
unit_test(test_concatenate),

unit_test(test_substring_overshoot),
unit_test(test_substring_result_is_one_char),
unit_test(test_substring_empty_input),
unit_test(test_substring_empty_output),
unit_test(test_substring_positive),
unit_test(test_substring_negative_length),
unit_test(test_substring_negative),
Expand Down
Loading