Fix off-by-one error in PGTYPEStimestamp_fmt_asc
authorTomas Vondra <tomas.vondra@postgresql.org>
Sat, 30 Nov 2019 13:51:27 +0000 (14:51 +0100)
committerTomas Vondra <tomas.vondra@postgresql.org>
Sat, 30 Nov 2019 14:16:58 +0000 (15:16 +0100)
commitd9b974e9947e54a9a55639f8f5f746bb5ad497b8
treeb6468ab3906a3d2abbf337ace3411512ab23f603
parent304ea5d98ed8c5b4d7ade7524a7d82841d335181
Fix off-by-one error in PGTYPEStimestamp_fmt_asc

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: http://postgr.es/m/16143-0d861eb8688d3fef%40postgresql.org
src/interfaces/ecpg/pgtypeslib/timestamp.c
src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.c
src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stderr
src/interfaces/ecpg/test/expected/pgtypeslib-dt_test.stdout
src/interfaces/ecpg/test/pgtypeslib/dt_test.pgc