Skip to content

Commit 11ccd16

Browse files
committed
PG-2540 Fix bug with overread after comment end in extraction
After the end of a comment we skipped one extra byte ahead accidentally causing a potential buffer overread if that comment was at the very end and otherwise potentially missing a comment if we had two comments next to eachother.
1 parent a6eb046 commit 11ccd16

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,8 +3220,10 @@ extract_query_comments(const char *query, char *comments, size_t buf_len)
32203220
q_iter++;
32213221
}
32223222
}
3223-
3224-
q_iter++;
3223+
else
3224+
{
3225+
q_iter++;
3226+
}
32253227
}
32263228

32273229
comments[curr_len] = '\0';

t/003_settings_pgms_extract_comments.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959
extra_params => [ '-a', '-Pformat=aligned', '-Ptuples_only=off' ]);
6060
PGSM::append_to_file($stdout);
6161

62+
($cmdret, $stdout, $stderr) = $node->psql(
63+
'postgres',
64+
'SELECT 1 /* a *//* b */, 2, 3',
65+
extra_params => [ '-a', '-Pformat=aligned', '-Ptuples_only=off' ]);
66+
PGSM::append_to_file($stdout);
67+
68+
($cmdret, $stdout, $stderr) = $node->psql(
69+
'postgres',
70+
'SELECT 1, 2, 3, 4 /* x */',
71+
extra_params => [ '-a', '-Pformat=aligned', '-Ptuples_only=off' ]);
72+
PGSM::append_to_file($stdout);
73+
6274
($cmdret, $stdout, $stderr) = $node->psql(
6375
'postgres',
6476
'SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";',

t/expected/003_settings_pgms_extract_comments.out

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,28 @@ SELECT 1 /* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2323
1 | 2
2424
(1 row)
2525

26+
SELECT 1 /* a *//* b */, 2, 3
27+
?column? | ?column? | ?column?
28+
----------+----------+----------
29+
1 | 2 | 3
30+
(1 row)
31+
32+
SELECT 1, 2, 3, 4 /* x */
33+
?column? | ?column? | ?column? | ?column?
34+
----------+----------+----------+----------
35+
1 | 2 | 3 | 4
36+
(1 row)
37+
2638
SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";
2739
query | comments
2840
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
41+
SELECT 1 /* a *//* b */, 2, 3 | /* a */
2942
SELECT 1 /* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */, 2 | /* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3043
SELECT 1 AS num /* First comment */, 'John' AS name /* Second comment*/ | /* First comment */, /* Second comment*/
44+
SELECT 1, 2, 3, 4 /* x */ | /* x */
3145
SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name = 'pg_stat_monitor.pgsm_extract_comments' |
3246
SELECT pg_stat_monitor_reset() |
33-
(4 rows)
47+
(6 rows)
3448

3549
SELECT pg_stat_monitor_reset();
3650
pg_stat_monitor_reset

0 commit comments

Comments
 (0)