Skip to content

Commit 71edbb6

Browse files
committed
Avoid use of non-portable strnlen() in pgstat_clip_activity().
The use of strnlen rather than strlen was just paranoia. Instead of giving up on the paranoia, just implement the safeguard differently. And add a comment explaining why we're careful. Author: Andres Freund Discussion: https://postgr.es/m/E1duOkJ-0001Mc-U5@gemulon.postgresql.org
1 parent 54b6cd5 commit 71edbb6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6288,10 +6288,24 @@ pgstat_db_requested(Oid databaseid)
62886288
* freed.
62896289
*/
62906290
char *
6291-
pgstat_clip_activity(const char *activity)
6291+
pgstat_clip_activity(const char *raw_activity)
62926292
{
6293-
int rawlen = strnlen(activity, pgstat_track_activity_query_size - 1);
6294-
int cliplen;
6293+
char *activity;
6294+
int rawlen;
6295+
int cliplen;
6296+
6297+
/*
6298+
* Some callers, like pgstat_get_backend_current_activity(), do not
6299+
* guarantee that the buffer isn't concurrently modified. We try to take
6300+
* care that the buffer is always terminated by a NULL byte regardless,
6301+
* but let's still be paranoid about the string's length. In those cases
6302+
* the underlying buffer is guaranteed to be
6303+
* pgstat_track_activity_query_size large.
6304+
*/
6305+
activity = pnstrdup(raw_activity, pgstat_track_activity_query_size - 1);
6306+
6307+
/* now double-guaranteed to be NULL terminated */
6308+
rawlen = strlen(activity);
62956309

62966310
/*
62976311
* All supported server-encodings make it possible to determine the length
@@ -6303,5 +6317,8 @@ pgstat_clip_activity(const char *activity)
63036317
*/
63046318
cliplen = pg_mbcliplen(activity, rawlen,
63056319
pgstat_track_activity_query_size - 1);
6306-
return pnstrdup(activity, cliplen);
6320+
6321+
activity[cliplen] = '\0';
6322+
6323+
return activity;
63076324
}

src/include/pgstat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
11991199

12001200
extern void pgstat_initstats(Relation rel);
12011201

1202-
extern char *pgstat_clip_activity(const char *activity);
1202+
extern char *pgstat_clip_activity(const char *raw_activity);
12031203

12041204
/* ----------
12051205
* pgstat_report_wait_start() -

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy