From c03fb02f44a06b8d30203f3567290f0bd70e3c73 Mon Sep 17 00:00:00 2001 From: simon987 Date: Wed, 15 Apr 2020 19:58:22 -0400 Subject: [PATCH] More text tests simon987/sist2#44 --- libscan/text/text.c | 4 +++- test/main.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libscan/text/text.c b/libscan/text/text.c index 1d3f1db..62ae442 100644 --- a/libscan/text/text.c +++ b/libscan/text/text.c @@ -30,7 +30,7 @@ scan_code_t parse_markup(scan_text_ctx_t *ctx, vfile_t *f, document_t *doc) { int to_read = MIN(MAX_MARKUP_SIZE, f->info.st_size); - char *buf = malloc(to_read); + char *buf = malloc(to_read + 1); int ret = f->read(f, buf, to_read); if (ret < 0) { CTX_LOG_ERRORF(doc->filepath, "read() returned error code: [%d]", ret) @@ -38,6 +38,8 @@ scan_code_t parse_markup(scan_text_ctx_t *ctx, vfile_t *f, document_t *doc) { return SCAN_ERR_READ; } + *(buf + to_read) = '\0'; + text_buffer_t tex = text_buffer_create(ctx->content_size); text_buffer_append_markup(&tex, buf); text_buffer_terminate_string(&tex); diff --git a/test/main.cpp b/test/main.cpp index d325c30..90d4143 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -99,6 +99,25 @@ TEST(Text, MemWhitespace) { cleanup(&doc, &f); } +TEST(Text, MemNoise) { + char content[600]; + + for (char &i : content) { + int x = rand(); + i = x == 0 ? 1 : x; + } + content[599] = '\0'; + + vfile_t f; + document_t doc; + load_doc_mem((void *) content, strlen(content), &f, &doc); + + parse_text(&text_500_ctx, &f, &doc); + + ASSERT_TRUE(utf8valid(get_meta(&doc, MetaContent)->str_val) == 0); + cleanup(&doc, &f); +} + TEST(TextMarkup, Mem1) { const char *content = "<test<>test test <>"; vfile_t f;