aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Johnson <nick@nicksphere.ch>2022-01-18 00:00:00 +0000
committerNicholas Johnson <nick@nicksphere.ch>2022-01-18 00:00:00 +0000
commit38e879561e71b4aafcd4478356cf21434287f197ad7f2260c3644b7b3b699121 (patch)
treee45f3d65736a1c0dc878a5678e679918128ac196a0db0a497e2847c9a82060df
parentacef5e1fc1f1dc7a7bcc15f7435c0fe450da4f3c9ca370a643fe5c199f3a70f6 (diff)
Patch escape_text_add_tags() crash on empty escaped string
-rw-r--r--src/gemini2html.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/gemini2html.c b/src/gemini2html.c
index d40a3bd..40621a0 100644
--- a/src/gemini2html.c
+++ b/src/gemini2html.c
@@ -98,6 +98,8 @@ void skip_non_whitespace(const char* str, size_t* pos) {
* Examples:
* escape_text_add_tags("<p>", "5 < 6\n", "</p>", 6) => "<p>5 &lt; 6</p>\n"
* escape_text_add_tags("<p>", "5 < 6", "</p>", 5) => "<p>5 &lt; 6</p>\n"
+ * escape_text_add_tags("", "", "", 0) => "\n"
+ * escape_text_add_tags("", "", "<br/>", 0) => "<br/>\n"
*/
char* escape_text_add_tags(const char* opening_tag, const char* unescaped, const char* closing_tag, const size_t unescaped_len) {
char* escaped = escape_text(unescaped, unescaped_len);
@@ -113,11 +115,12 @@ char* escape_text_add_tags(const char* opening_tag, const char* unescaped, const
strncpy(escaped_with_tags + index, opening_tag, opening_tag_len);
index += opening_tag_len;
- /* append escaped text without newline at the end, if one exists */
- strncpy(escaped_with_tags + index, escaped, escaped_len - 1);
+ /* append escaped text */
+ strncpy(escaped_with_tags + index, escaped, escaped_len);
+
+ /* don't increment index if newline so newline can be overwritten */
index += escaped_len - 1;
- bool is_newline = escaped[escaped_len - 1] == '\n';
- if (!is_newline) escaped_with_tags[index++] = escaped[escaped_len - 1];
+ if (escaped_with_tags[index] != '\n') index++;
/* escaped string is no longer needed */
free(escaped);
@@ -154,7 +157,7 @@ enum linetype getlinetype(const char* line, const bool pre_is_toggled) {
}
char* convert_text_line(const char* line, const size_t size) {
- if (line[0] == '\n') return strdup("<br/>\n");
+ if (line[0] == '\n') return escape_text_add_tags("", "", "<br/>", 0);
else return escape_text_add_tags("<p>", line, "</p>", size);
}