diff options
author | dx <aryadevchavali1@gmail.com> | 2020-05-07 01:21:10 +0100 |
---|---|---|
committer | dx <aryadevchavali1@gmail.com> | 2020-05-07 01:21:10 +0100 |
commit | 991acdace4cce69c816b1ca661d87e3affa2258c (patch) | |
tree | e71812c082e5d98c41de17852a786c316911f97a /Converter | |
parent | eeee4a1bf55434d168b3c07eab8cf156bc54ea13 (diff) | |
download | mdhtml-991acdace4cce69c816b1ca661d87e3affa2258c.tar.gz mdhtml-991acdace4cce69c816b1ca661d87e3affa2258c.tar.bz2 mdhtml-991acdace4cce69c816b1ca661d87e3affa2258c.zip |
~correcting my intuition about c-strings
On converting, I would sometimes get the other tag missing. This was
both on the repl and the tests, and was quite annoying.
Each c-string should end in a null terminator, which I forgot about. So
this commit fixes that by setting the last character pointer on each
branch to the null terminator.
Diffstat (limited to 'Converter')
-rw-r--r-- | Converter/src/compiler.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c index aa5dc58..7efa032 100644 --- a/Converter/src/compiler.c +++ b/Converter/src/compiler.c @@ -126,14 +126,15 @@ string compile_line(string line, size_t size_of_line, string line_or_source) // allocate buffer with extra 9 characters for the tags compiled_line = malloc(sizeof(*compiled_line) * - (size_of_line + tag_size + close_tag_size + 3)); + (size_of_line + tag_size + close_tag_size + 1)); // write h1 to start of compiled_line strncpy(compiled_line, tag, tag_size); // write the rest of the line to the compiled_line strncpy(compiled_line + tag_size, line + depth, size_of_line); // write the end tags - strncpy(compiled_line + size_of_content + tag_size - 1, close_tag, - close_tag_size + 1); + strncpy(compiled_line + size_of_content + tag_size, close_tag, + close_tag_size); + compiled_line[size_of_content + tag_size + close_tag_size] = '\0'; } else if (line[0] == '-') // Compile a list item { @@ -141,10 +142,11 @@ string compile_line(string line, size_t size_of_line, string line_or_source) string close_tag = "</li>"; size_of_content = size_of_line - 1; - compiled_line = malloc(sizeof(*compiled_line) * (size_of_line + 10)); + compiled_line = malloc(sizeof(*compiled_line) * (size_of_content + 10)); strncpy(compiled_line, tag, 4); strncpy(compiled_line + 4, line + 1, size_of_line); strncpy(compiled_line + size_of_content + 4, close_tag, 5); + compiled_line[size_of_content + 9] = '\0'; // terminate string } else // Compile a standard piece of text { |