From 2f8f463a469af945f7997d0756bfea3213a1c71d Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Mon, 15 Feb 2021 19:31:09 +0100 Subject: [PATCH] Fix length argument to pcre2_substitute() --- src/Makefile | 2 +- src/regulex.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Makefile b/src/Makefile index 759a979..6679304 100644 --- a/src/Makefile +++ b/src/Makefile @@ -85,7 +85,7 @@ unicode.o: unicode.c unicode.h boxes.h tools.h config.h shape.o: shape.c shape.h boxes.h tools.h config.h generate.o: generate.c generate.h boxes.h shape.h tools.h unicode.h config.h remove.o: remove.c remove.h boxes.h shape.h tools.h unicode.h config.h -regulex.o: regulex.c regulex.h tools.h unicode.h config.h +regulex.o: regulex.c regulex.h boxes.h tools.h unicode.h config.h lex.yy.o: lex.yy.c parser.h tools.h shape.h lexer.h config.h parser.o: parser.c parser.h tools.h shape.h lexer.h config.h misc/getopt.o: misc/getopt.c diff --git a/src/regulex.c b/src/regulex.c index 2ceca4a..66855e1 100644 --- a/src/regulex.c +++ b/src/regulex.c @@ -28,6 +28,7 @@ #include #include +#include "boxes.h" #include "tools.h" #include "unicode.h" #include "regulex.h" @@ -66,13 +67,15 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons { PCRE2_SPTR replacement = u32_strconv_from_arg(replace, config_encoding); if (replacement == NULL) { + fprintf(stderr, "Failed to convert replacement string to UTF-32 - \"%s\"\n", replace); return NULL; } + uint32_t options = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH | PCRE2_SUBSTITUTE_EXTENDED | (global ? PCRE2_SUBSTITUTE_GLOBAL : 0); PCRE2_SIZE outlen = input_len * 2; /* estimated length of output buffer in characters, fine if too small */ - PCRE2_SIZE bufsize = (input_len == 0) ? 16 : outlen; + PCRE2_SIZE bufsize = (input_len < 8) ? 16 : outlen; uint32_t *output = (uint32_t *) malloc(sizeof(uint32_t) * bufsize); /* output buffer */ int pcre2_rc; @@ -84,7 +87,8 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons } PCRE2_SIZE outlen = bufsize; - pcre2_rc = pcre2_substitute(search, (PCRE2_SPTR) input, input_len, + pcre2_rc = pcre2_substitute(search, + (PCRE2_SPTR) input, PCRE2_ZERO_TERMINATED, 0, /* start offset */ options, NULL, /* ptr to a match data block */