Skip to content

Commit a762f7b

Browse files
var77hackorum
authored andcommitted
contrib/xml2: guard against signed integer overflow in parse_params
The doubling of max_params in parse_params relies on signed integer overflow to wrap to a negative value that AllocSizeIsValid then rejects, producing a clean ereport. This is incidental safety: signed overflow is undefined per the C standard, and the graceful ERROR depends on the wrapped value falling outside MaxAllocSize after promotion to size_t. In current builds the overflow is unreachable, since text input is bounded by MaxAllocSize and that limits nparams below the doubling threshold. Guard the multiplication anyway, matching the explicit overflow-checking idiom used elsewhere in the tree.
1 parent 127ce77 commit a762f7b

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

contrib/xml2/xslt_proc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include "postgres.h"
99

10+
#include "common/int.h"
1011
#include "fmgr.h"
1112
#include "utils/builtins.h"
1213
#include "utils/xml.h"
@@ -223,6 +224,7 @@ parse_params(text *paramstr)
223224
char *itsep = ",";
224225
const char **params;
225226
int max_params;
227+
int new_max_params;
226228
int nparams;
227229

228230
pstr = text_to_cstring(paramstr);
@@ -237,7 +239,12 @@ parse_params(text *paramstr)
237239
{
238240
if (nparams >= max_params)
239241
{
240-
max_params *= 2;
242+
if (pg_mul_s32_overflow(max_params, 2, &new_max_params))
243+
ereport(ERROR,
244+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
245+
errmsg("too many XSLT parameters")));
246+
247+
max_params = new_max_params;
241248
params = (const char **) repalloc(params,
242249
(max_params + 1) * sizeof(char *));
243250
}

0 commit comments

Comments
 (0)