Skip to content

Commit

Permalink
Fix length argument to pcre2_substitute()
Browse files Browse the repository at this point in the history
  • Loading branch information
tsjensen committed Feb 15, 2021
1 parent b0d2dd8 commit 2f8f463
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/regulex.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdlib.h>
#include <string.h>

#include "boxes.h"
#include "tools.h"
#include "unicode.h"
#include "regulex.h"
Expand Down Expand Up @@ -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;

Expand All @@ -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 */
Expand Down

0 comments on commit 2f8f463

Please sign in to comment.