Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e77a1c5

Browse files
committed
ecpg: Fix zero-termination of string generated by intoasc()
intoasc(), a wrapper for PGTYPESinterval_to_asc that converts an interval to its textual representation, used a plain memcpy() when copying its result. This could miss a zero-termination in the result string, leading to an incorrect result. The routines in informix.c do not provide the length of their result buffer, which would allow a replacement of strcpy() to safer strlcpy() calls, but this requires an ABI breakage and that cannot happen in back-branches. Author: Oleg Tselebrovskiy Reviewed-by: Ashutosh Bapat Discussion: https://postgr.es/m/bf47888585149f83b276861a1662f7e4@postgrespro.ru Backpatch-through: 12
1 parent 0a9118c commit e77a1c5

File tree

9 files changed

+70
-2
lines changed

9 files changed

+70
-2
lines changed

src/interfaces/ecpg/compatlib/informix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ intoasc(interval * i, char *str)
654654
if (!tmp)
655655
return -errno;
656656

657-
memcpy(str, tmp, strlen(tmp));
657+
strcpy(str, tmp);
658658
free(tmp);
659659
return 0;
660660
}

src/interfaces/ecpg/test/compat_informix/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/dec_test.c
55
/describe
66
/describe.c
7+
/intoasc
8+
/intoasc.c
79
/rfmtdate
810
/rfmtdate.c
911
/rfmtlong

src/interfaces/ecpg/test/compat_informix/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ TESTS = test_informix test_informix.c \
1616
rnull rnull.c \
1717
sqlda sqlda.c \
1818
describe describe.c \
19-
charfuncs charfuncs.c
19+
charfuncs charfuncs.c \
20+
intoasc intoasc.c
2021

2122
all: $(TESTS)
2223

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "pgtypes_interval.h"
5+
6+
EXEC SQL BEGIN DECLARE SECTION;
7+
char dirty_str[100] = "aaaaaaaaa_bbbbbbbb_ccccccccc_ddddddddd_";
8+
interval *interval_ptr;
9+
EXEC SQL END DECLARE SECTION;
10+
11+
int main()
12+
{
13+
interval_ptr = (interval *) malloc(sizeof(interval));
14+
interval_ptr->time = 100000000;
15+
interval_ptr->month = 240;
16+
17+
printf("dirty_str contents before intoasc: %s\n", dirty_str);
18+
intoasc(interval_ptr, dirty_str);
19+
printf("dirty_str contents after intoasc: %s\n", dirty_str);
20+
return 0;
21+
}

src/interfaces/ecpg/test/compat_informix/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pgc_files = [
44
'charfuncs',
55
'dec_test',
66
'describe',
7+
'intoasc',
78
'rfmtdate',
89
'rfmtlong',
910
'rnull',

src/interfaces/ecpg/test/ecpg_schedule

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test: compat_informix/sqlda
77
test: compat_informix/describe
88
test: compat_informix/test_informix
99
test: compat_informix/test_informix2
10+
test: compat_informix/intoasc
1011
test: compat_oracle/char_array
1112
test: connect/test2
1213
test: connect/test3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Processed by ecpg (regression mode) */
2+
/* These include files are added by the preprocessor */
3+
#include <ecpglib.h>
4+
#include <ecpgerrno.h>
5+
#include <sqlca.h>
6+
/* Needed for informix compatibility */
7+
#include <ecpg_informix.h>
8+
/* End of automatic include section */
9+
#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
10+
11+
#line 1 "intoasc.pgc"
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
15+
#include "pgtypes_interval.h"
16+
17+
/* exec sql begin declare section */
18+
19+
20+
21+
#line 7 "intoasc.pgc"
22+
char dirty_str [ 100 ] = "aaaaaaaaa_bbbbbbbb_ccccccccc_ddddddddd_" ;
23+
24+
#line 8 "intoasc.pgc"
25+
interval * interval_ptr ;
26+
/* exec sql end declare section */
27+
#line 9 "intoasc.pgc"
28+
29+
30+
int main()
31+
{
32+
interval_ptr = (interval *) malloc(sizeof(interval));
33+
interval_ptr->time = 100000000;
34+
interval_ptr->month = 240;
35+
36+
printf("dirty_str contents before intoasc: %s\n", dirty_str);
37+
intoasc(interval_ptr, dirty_str);
38+
printf("dirty_str contents after intoasc: %s\n", dirty_str);
39+
return 0;
40+
}

src/interfaces/ecpg/test/expected/compat_informix-intoasc.stderr

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dirty_str contents before intoasc: aaaaaaaaa_bbbbbbbb_ccccccccc_ddddddddd_
2+
dirty_str contents after intoasc: @ 20 years 1 min 40 secs

0 commit comments

Comments
 (0)