git.postgresql.org Git - postgresql.git/commitdiff

git projects / postgresql.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 79d6e6a)
Fix off-by-one error in PGTYPEStimestamp_fmt_asc
2019年11月30日 13:51:27 +0000 (14:51 +0100)
2019年11月30日 14:04:02 +0000 (15:04 +0100)
When using %b or %B patterns to format a date, the code was simply using
tm_mon as an index into array of month names. But that is wrong, because
tm_mon is 1-based, while array indexes are 0-based. The result is we
either use name of the next month, or a segfault (for December).

Fix by subtracting 1 from tm_mon for both patterns, and add a regression
test triggering the issue. Backpatch to all supported versions (the bug
is there far longer, since at least 2003).

Reported-by: Paul Spencer
Backpatch-through: 9.4
Discussion: https://postgr.es/m/16143-0d861eb8688d3fef%40postgresql.org


diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c
index efd9f3aa7198ce0c0db7eec3aadbc4eb8e0563f8..afc5c54b5594eb9f9f97f5e90aa6febb30bd5051 100644 (file)
--- a/src/interfaces/ecpg/pgtypeslib/timestamp.c
+++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c
@@ -336,13 +336,13 @@ dttofmtasc_replace(timestamp * ts, date dDate, int dow, struct tm *tm,
/* XXX should be locale aware */
case 'b':
case 'h':
- replace_val.str_val = months[tm->tm_mon];
+ replace_val.str_val = months[tm->tm_mon - 1];
replace_type = PGTYPES_TYPE_STRING_CONSTANT;
break;
/* the full name of the month */
/* XXX should be locale aware */
case 'B':
- replace_val.str_val = pgtypes_date_months[tm->tm_mon];
+ replace_val.str_val = pgtypes_date_months[tm->tm_mon - 1];
replace_type = PGTYPES_TYPE_STRING_CONSTANT;
break;
diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.c b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.c
index 1509a56fd85ae6dbc65de42ae54767c2a8ba3a62..ee867b6dd862ec79880aea21462f590f078fc320 100644 (file)
--- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.c
+++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.c
@@ -436,17 +436,33 @@ if (sqlca.sqlcode < 0) sqlprint ( );}
printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
PGTYPESchar_free(text);
+ out = (char*) malloc(64);
+ fmt = "%a %b %d %H:%M:%S %Y";
+ in = "Mon Dec 30 17:28:44 2019";
+ i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
+ i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
+ printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
+ free(out);
+
+ out = (char*) malloc(64);
+ fmt = "%a %b %d %H:%M:%S %Y";
+ in = "Mon December 30 17:28:44 2019";
+ i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
+ i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
+ printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
+ free(out);
+
{ ECPGtrans(__LINE__, NULL, "rollback");
-#line 365 "dt_test.pgc"
+#line 381 "dt_test.pgc"
if (sqlca.sqlcode < 0) sqlprint ( );}
-#line 365 "dt_test.pgc"
+#line 381 "dt_test.pgc"
{ ECPGdisconnect(__LINE__, "CURRENT");
-#line 366 "dt_test.pgc"
+#line 382 "dt_test.pgc"
if (sqlca.sqlcode < 0) sqlprint ( );}
-#line 366 "dt_test.pgc"
+#line 382 "dt_test.pgc"
return 0;
diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stderr b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stderr
index b6dc22688b528a33a87a6a21b73c583b8e52229c..6e9ed3d3dbad360239f7cae32056345683ff084b 100644 (file)
--- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stderr
+++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stderr
@@ -42,7 +42,7 @@
[NO_PID]: sqlca: code: 0, state: 00000
[NO_PID]: ecpg_get_data on line 38: RESULT: 2000年07月12日 17:34:29 offset: -1; array: no
[NO_PID]: sqlca: code: 0, state: 00000
-[NO_PID]: ECPGtrans on line 365: action "rollback"; connection "ecpg1_regression"
+[NO_PID]: ECPGtrans on line 381: action "rollback"; connection "ecpg1_regression"
[NO_PID]: sqlca: code: 0, state: 00000
[NO_PID]: ecpg_finish: connection ecpg1_regression closed
[NO_PID]: sqlca: code: 0, state: 00000
diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stdout b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stdout
index 823b6e0062352a2f072f86fab86673603c4948ea..4b582fd7a28029c5a229db27ed59241b786cf590 100644 (file)
--- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stdout
+++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stdout
@@ -48,3 +48,5 @@ timestamp_defmt_asc( 1976, July 14. Time: 9:15am, %Y, %B %d. Time: %I:%M %p)
timestamp_defmt_asc( 1976, July 14. Time: 9:15 am, %Y, %B %d. Time: %I:%M%p) = 1976年07月14日 09:15:00, error: 0
timestamp_defmt_asc( 1976, P.M. July 14. Time: 9:15, %Y, %P %B %d. Time: %I:%M) = 1976年07月14日 21:15:00, error: 0
timestamp_defmt_asc(1234567890, %s) = 2009年02月13日 23:31:30, error: 0
+timestamp_defmt_asc(Mon Dec 30 17:28:44 2019, %a %b %d %H:%M:%S %Y) = Mon Dec 30 17:28:44 2019, error: 0
+timestamp_defmt_asc(Mon December 30 17:28:44 2019, %a %b %d %H:%M:%S %Y) = Mon Dec 30 17:28:44 2019, error: 0
diff --git a/src/interfaces/ecpg/test/pgtypeslib/dt_test.pgc b/src/interfaces/ecpg/test/pgtypeslib/dt_test.pgc
index 95632fffeefe775115370bb0c64a68ff8d97ac4c..f81a3926655055d8145b55a14675c05c5068a821 100644 (file)
--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test.pgc
+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test.pgc
@@ -362,6 +362,22 @@ main(void)
printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
PGTYPESchar_free(text);
+ out = (char*) malloc(64);
+ fmt = "%a %b %d %H:%M:%S %Y";
+ in = "Mon Dec 30 17:28:44 2019";
+ i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
+ i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
+ printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
+ free(out);
+
+ out = (char*) malloc(64);
+ fmt = "%a %b %d %H:%M:%S %Y";
+ in = "Mon December 30 17:28:44 2019";
+ i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
+ i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
+ printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
+ free(out);
+
exec sql rollback;
exec sql disconnect;
This is the main PostgreSQL git repository.
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /