Skip to content

Commit 167cb26

Browse files
michaelpqsamimseih
andcommitted
Fix const correctness in pgstat data serialization callbacks
4ba012a defined the "header" (pointer to the stats data) of from_serialized_data() as a const, even though it is fine (and expected!) for the callback to modify the shared memory entry when loading the stats at startup. While on it, this commit updates the callback to_serialized_data() in the test module test_custom_stats to make the data extracted from the "header" parameter a const since it should never be modified: the stats are written to disk and no modifications are expected in the shared memory entry. This clarifies the API contract of these new callbacks. Reported-By: Peter Eisentraut <peter@eisentraut.org> Author: Michael Paquier <michael@paquier.xyz> Co-authored-by: Sami Imseih <samimseih@gmail.com> Discussion: https://postgr.es/m/d87a93b0-19c7-4db6-b9c0-d6827e7b2da1@eisentraut.org
1 parent ab8af1d commit 167cb26

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/include/utils/pgstat_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,14 @@ typedef struct PgStat_KindInfo
329329
*
330330
* "statfile" is a pointer to the on-disk stats file, named
331331
* PGSTAT_STAT_PERMANENT_FILENAME. "key" is the hash key of the entry
332-
* just written or read. "header" is a pointer to the stats data.
332+
* just written or read. "header" is a pointer to the stats data; it may
333+
* be modified only in from_serialized_data to reconstruct an entry.
333334
*/
334335
void (*to_serialized_data) (const PgStat_HashKey *key,
335336
const PgStatShared_Common *header,
336337
FILE *statfile);
337338
bool (*from_serialized_data) (const PgStat_HashKey *key,
338-
const PgStatShared_Common *header,
339+
PgStatShared_Common *header,
339340
FILE *statfile);
340341

341342
/*

src/test/modules/test_custom_stats/test_custom_var_stats.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
9292

9393
/* Deserialization callback: read auxiliary entry data */
9494
static bool test_custom_stats_var_from_serialized_data(const PgStat_HashKey *key,
95-
const PgStatShared_Common *header,
95+
PgStatShared_Common *header,
9696
FILE *statfile);
9797

9898
/* Finish callback: end of statistics file operations */
@@ -196,7 +196,7 @@ test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
196196
{
197197
char *description;
198198
size_t len;
199-
PgStatShared_CustomVarEntry *entry = (PgStatShared_CustomVarEntry *) header;
199+
const PgStatShared_CustomVarEntry *entry = (const PgStatShared_CustomVarEntry *) header;
200200
bool found;
201201
uint32 magic_number = TEST_CUSTOM_VAR_MAGIC_NUMBER;
202202

@@ -276,7 +276,7 @@ test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
276276
*/
277277
static bool
278278
test_custom_stats_var_from_serialized_data(const PgStat_HashKey *key,
279-
const PgStatShared_Common *header,
279+
PgStatShared_Common *header,
280280
FILE *statfile)
281281
{
282282
PgStatShared_CustomVarEntry *entry;

0 commit comments

Comments
 (0)