Skip to content

Commit 775a97d

Browse files
committed
Get rid of hacky usage of pgut_strdup() in utoa.
From inspecting the call sites of utoa, it appears that some of them (especially the recent cleanup patch which added the strdup there) wanted to prevent overwriting a local variable by repeated call (to utoa) using the same variable as argument. This commit instead makes such call sites strdup the variable itself before passing it to utoa. That seems cleaner considering that it does not seem utoa's contract to do so (strdup its parameter that is).
1 parent d3a99db commit 775a97d

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

bin/pg_repack.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,8 @@ static char *
243243
utoa(unsigned int value, char *buffer)
244244
{
245245
sprintf(buffer, "%u", value);
246-
/* XXX: originally, we would just return buffer here without
247-
* the pgut_strdup(). But repack_cleanup_callback() seems to
248-
* depend on getting back a freshly strdup'd copy of buffer,
249-
* not sure why. So now we are leaking a tiny bit of memory
250-
* with each utoa() call.
251-
*/
252-
return pgut_strdup(buffer);
246+
247+
return buffer;
253248
}
254249

255250
static pgut_option options[] =
@@ -1397,7 +1392,7 @@ repack_one_table(repack_table *table, const char *orderby)
13971392
elog(DEBUG2, "---- drop ----");
13981393

13991394
command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL);
1400-
params[1] = utoa(temp_obj_num, buffer);
1395+
params[1] = utoa(temp_obj_num, pgut_strdup(buffer));
14011396
command("SELECT repack.repack_drop($1, $2)", 2, params);
14021397
command("COMMIT", 0, NULL);
14031398
temp_obj_num = 0; /* reset temporary object counter after cleanup */
@@ -1419,7 +1414,7 @@ repack_one_table(repack_table *table, const char *orderby)
14191414

14201415
/* Release advisory lock on table. */
14211416
params[0] = REPACK_LOCK_PREFIX_STR;
1422-
params[1] = utoa(table->target_oid, buffer);
1417+
params[1] = utoa(table->target_oid, pgut_strdup(buffer));
14231418

14241419
res = pgut_execute(connection, "SELECT pg_advisory_unlock($1, CAST(-2147483648 + $2::bigint AS integer))",
14251420
2, params);
@@ -1712,7 +1707,7 @@ repack_cleanup_callback(bool fatal, void *userdata)
17121707
if(fatal)
17131708
{
17141709
params[0] = utoa(target_table, buffer);
1715-
params[1] = utoa(temp_obj_num, buffer);
1710+
params[1] = utoa(temp_obj_num, pgut_strdup(buffer));
17161711

17171712
/* testing PQstatus() of connection and conn2, as we do
17181713
* in repack_cleanup(), doesn't seem to work here,
@@ -1747,7 +1742,7 @@ repack_cleanup(bool fatal, const repack_table *table)
17471742

17481743
/* do cleanup */
17491744
params[0] = utoa(table->target_oid, buffer);
1750-
params[1] = utoa(temp_obj_num, buffer);
1745+
params[1] = utoa(temp_obj_num, pgut_strdup(buffer));
17511746
command("SELECT repack.repack_drop($1, $2)", 2, params);
17521747
temp_obj_num = 0; /* reset temporary object counter after cleanup */
17531748
}

0 commit comments

Comments
 (0)