setexpr: Add some tests for buffer overflow and backref

Add tests to check for buffer overflow using simple replacement as well
as back references. At present these don't fully pass.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2020-11-01 14:15:40 -07:00 committed by Tom Rini
parent 56331b2680
commit d422c77ae8
3 changed files with 110 additions and 17 deletions

View file

@ -134,22 +134,8 @@ static char *substitute(char *string, int *slen, int ssize,
return p + nlen;
}
/**
* regex_sub() - Replace a regex pattern with a string
*
* @data: Buffer containing the string to update
* @data_size: Size of buffer (must be large enough for the new string)
* @nbuf: Back-reference buffer
* @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
* all back-reference expansions)
* @r: Regular expression to find
* @s: String to replace with
* @global: true to replace all matches in @data, false to replace just the
* first
* @return 0 if OK, 1 on error
*/
static int regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
const char *r, const char *s, bool global)
int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
const char *r, const char *s, bool global)
{
struct slre slre;
char *datap = data;
@ -325,7 +311,8 @@ static int regex_sub_var(const char *name, const char *r, const char *s,
strcpy(data, t);
ret = regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, global);
ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s,
global);
if (ret)
return 1;