Skip to content

Commit

Permalink
fix: heap-buffer-overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Oct 2, 2023
1 parent e5dad3f commit 9406e55
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions misc/run_length_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ char* run_length_encode(char* str) {
int encoded_index = 0;

//allocate space for worst-case scenario
char* encoded = malloc(2 * strlen(str));
char* encoded = malloc(2 * strlen(str)+1);

//temp space for int to str conversion
char int_str[20];
Expand All @@ -54,9 +54,10 @@ char* run_length_encode(char* str) {
++encoded_index;
}

//null terminate string and move encoded string to compacted memory space
//null terminate string and move encoded string to compacted memory space
printf("%d %ld\n", encoded_index, 2 * strlen(str)+1);
encoded[encoded_index] = '\0';
char* compacted_string = malloc(strlen(encoded) + 1);
char* compacted_string = malloc(strlen(encoded)+1);
strcpy(compacted_string, encoded);

free(encoded);
Expand All @@ -74,11 +75,26 @@ static void test() {
assert(!strcmp(test, "7a3b2a4c1d1e1f2a1d1r"));
free(test);
test = run_length_encode("lidjhvipdurevbeirbgipeahapoeuhwaipefupwieofb");
assert(!strcmp(test, "1l1i1d1j1h1v1i1p1d1u1r1e1v1b1e1i1r1b1g1i1p1e1a1h1a1p1o1e1u1h1w1a1i1p1e1f1u1p1w1i1e1o1f1bq"));
assert(!strcmp(test, "1l1i1d1j1h1v1i1p1d1u1r1e1v1b1e1i1r1b1g1i1p1e1a1h1a1p1o1e1u1h1w1a1i1p1e1f1u1p1w1i1e1o1f1b"));
free(test);
test = run_length_encode("htuuuurwuquququuuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahghghrw");
assert(!strcmp(test, "1h1t4u1r1w1u1q1u1q1u1q3u76a1h1g1h1g1h1r1w"));
free(test);
test = run_length_encode("aaaa");
assert(!strcmp(test, "4a"));
free(test);
test = run_length_encode("aaa");
assert(!strcmp(test, "3a"));
free(test);
test = run_length_encode("aa");
assert(!strcmp(test, "2a"));
free(test);
test = run_length_encode("a");
assert(!strcmp(test, "1a"));
free(test);
test = run_length_encode("");
assert(!strcmp(test, ""));
free(test);
}

/**
Expand Down

0 comments on commit 9406e55

Please sign in to comment.