Skip to content

Commit 01e51ed

Browse files
committed
Replace static buf with a stack-allocated one in 'seg' extension
The buffer is used only locally within the function. Also, the initialization to '0' characters was unnecessary, the initial content were always overwritten with sprintf(). I don't understand why it was done that way, but it's been like that since forever. In the passing, change from sprintf() to snprintf(). The buffer was long enough so sprintf() was fine, but this makes it more obvious that there's no risk of a buffer overflow. Reviewed-by: Robert Haas Discussion: https://www.postgresql.org/message-id/7f86e06a-98c5-4ce3-8ec9-3885c8de0358@iki.fi
1 parent da8a587 commit 01e51ed

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

contrib/seg/segparse.y

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ static bool seg_atof(char *value, float *result, struct Node *escontext);
2929

3030
static int sig_digits(const char *value);
3131

32-
static char strbuf[25] = {
33-
'0', '0', '0', '0', '0',
34-
'0', '0', '0', '0', '0',
35-
'0', '0', '0', '0', '0',
36-
'0', '0', '0', '0', '0',
37-
'0', '0', '0', '0', '\0'
38-
};
39-
4032
%}
4133

4234
/* BISON Declarations */
@@ -69,11 +61,13 @@ static char strbuf[25] = {
6961

7062
range: boundary PLUMIN deviation
7163
{
64+
char strbuf[25];
65+
7266
result->lower = $1.val - $3.val;
7367
result->upper = $1.val + $3.val;
74-
sprintf(strbuf, "%g", result->lower);
68+
snprintf(strbuf, sizeof(strbuf), "%g", result->lower);
7569
result->l_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
76-
sprintf(strbuf, "%g", result->upper);
70+
snprintf(strbuf, sizeof(strbuf), "%g", result->upper);
7771
result->u_sigd = Max(sig_digits(strbuf), Max($1.sigd, $3.sigd));
7872
result->l_ext = '\0';
7973
result->u_ext = '\0';

0 commit comments

Comments
 (0)