Skip to content

Commit e5ba4f0

Browse files
gkokolatosGeorgios Kokolatos
andauthored
Plug minimal memleak in keeper's configuration (#583)
Admittedly this leak could have been plugged with a couple of well placed calls to the now obsolete keeper_config_destroy() function. However, given the lifecycle and globality of the struct the variable is found, ownership is blurred in some cases. Instead the leaking variable is now living in the stack along with the rest. A new variable for its length is added instead of MAXCONNINFO which seems a bit too excessive. Alignment concerns aside, some relief from the stack stress might be desirable. Finally, the necessity for keeper_config_destroy is now removed. Thus the function itself is removed for the benefit of the reader of the codebase. Co-authored-by: Georgios Kokolatos <gkokolatos@pm.com>
1 parent 238bfec commit e5ba4f0

8 files changed

Lines changed: 13 additions & 37 deletions

File tree

src/bin/pg_autoctl/cli_common.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,8 +1405,6 @@ cli_drop_local_node(KeeperConfig *config, bool dropAndDestroy)
14051405
log_info("HINT: to completely remove your local Postgres instance and "
14061406
"setup, consider `pg_autoctl drop node --destroy`");
14071407
}
1408-
1409-
keeper_config_destroy(config);
14101408
}
14111409

14121410

src/bin/pg_autoctl/cli_config.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ cli_config_check(int argc, char **argv)
221221
exit(EXIT_CODE_INTERNAL_ERROR);
222222
}
223223
}
224-
225-
keeper_config_destroy(&config);
226224
}
227225

228226

@@ -470,7 +468,6 @@ cli_keeper_config_get(int argc, char **argv)
470468
}
471469
}
472470

473-
keeper_config_destroy(&config);
474471
break;
475472
}
476473

@@ -493,7 +490,6 @@ cli_keeper_config_get(int argc, char **argv)
493490
exit(EXIT_CODE_BAD_ARGS);
494491
}
495492

496-
keeper_config_destroy(&config);
497493
break;
498494
}
499495

@@ -549,7 +545,6 @@ cli_monitor_config_get(int argc, char **argv)
549545
fformat(stdout, "\n");
550546
}
551547

552-
keeper_config_destroy(&kconfig);
553548
break;
554549
}
555550

@@ -572,7 +567,6 @@ cli_monitor_config_get(int argc, char **argv)
572567
exit(EXIT_CODE_BAD_ARGS);
573568
}
574569

575-
keeper_config_destroy(&kconfig);
576570
break;
577571
}
578572

@@ -680,8 +674,6 @@ cli_keeper_config_set(int argc, char **argv)
680674
log_error("Failed to lookup option %s", argv[0]);
681675
exit(EXIT_CODE_BAD_ARGS);
682676
}
683-
684-
keeper_config_destroy(&config);
685677
}
686678
}
687679

@@ -742,7 +734,5 @@ cli_monitor_config_set(int argc, char **argv)
742734
log_error("Failed to lookup option %s", argv[0]);
743735
exit(EXIT_CODE_BAD_ARGS);
744736
}
745-
746-
keeper_config_destroy(&kconfig);
747737
}
748738
}

src/bin/pg_autoctl/defaults.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define GROUP_ID_DEFAULT 0
4141
#define POSTGRES_CONNECT_TIMEOUT "2"
4242
#define MAXIMUM_BACKUP_RATE "100M"
43+
#define MAXIMUM_BACKUP_RATE_LEN 32
4344

4445

4546
/*

src/bin/pg_autoctl/keeper.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,9 +2034,9 @@ keeper_config_accept_new(Keeper *keeper, KeeperConfig *newConfig)
20342034
"used to be \"%s\"",
20352035
newConfig->maximum_backup_rate, config->maximum_backup_rate);
20362036

2037-
/* note: strneq checks args are not NULL, it's safe to proceed */
2038-
free(config->maximum_backup_rate);
2039-
config->maximum_backup_rate = strdup(newConfig->maximum_backup_rate);
2037+
strlcpy(config->maximum_backup_rate,
2038+
newConfig->maximum_backup_rate,
2039+
MAXIMUM_BACKUP_RATE_LEN);
20402040
}
20412041

20422042
/*
@@ -2190,9 +2190,6 @@ keeper_reload_configuration(Keeper *keeper, bool firstLoop, bool doInit)
21902190
"continuing with the same configuration.",
21912191
config->pathnames.config);
21922192
}
2193-
2194-
/* we're done the newConfig now */
2195-
keeper_config_destroy(&newConfig);
21962193
}
21972194
else
21982195
{

src/bin/pg_autoctl/keeper_config.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@
133133
REPLICATION_PASSWORD_DEFAULT)
134134

135135
#define OPTION_REPLICATION_MAXIMUM_BACKUP_RATE(config) \
136-
make_string_option_default("replication", "maximum_backup_rate", NULL, \
137-
false, &config->maximum_backup_rate, \
136+
make_strbuf_option_default("replication", "maximum_backup_rate", NULL, \
137+
false, MAXIMUM_BACKUP_RATE_LEN, \
138+
config->maximum_backup_rate, \
138139
MAXIMUM_BACKUP_RATE)
139140

140141
#define OPTION_REPLICATION_BACKUP_DIR(config) \
@@ -684,19 +685,6 @@ keeper_config_update(KeeperConfig *config, int nodeId, int groupId)
684685
}
685686

686687

687-
/*
688-
* keeper_config_destroy frees memory that may be dynamically allocated.
689-
*/
690-
void
691-
keeper_config_destroy(KeeperConfig *config)
692-
{
693-
if (config->maximum_backup_rate != NULL)
694-
{
695-
free(config->maximum_backup_rate);
696-
}
697-
}
698-
699-
700688
/*
701689
* keeper_config_init_nodekind initializes the config->nodeKind and
702690
* config->pgSetup.pgKind values from the configuration file or command line

src/bin/pg_autoctl/keeper_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdbool.h>
1515

1616
#include "config.h"
17+
#include "defaults.h"
1718
#include "pgctl.h"
1819
#include "pgsql.h"
1920

@@ -40,7 +41,7 @@ typedef struct KeeperConfig
4041
/* PostgreSQL replication / tooling setup */
4142
char replication_slot_name[MAXCONNINFO];
4243
char replication_password[MAXCONNINFO];
43-
char *maximum_backup_rate;
44+
char maximum_backup_rate[MAXIMUM_BACKUP_RATE_LEN];
4445
char backupDirectory[MAXPGPATH];
4546

4647
/* pg_autoctl timeouts */
@@ -84,7 +85,6 @@ bool keeper_config_set_setting(KeeperConfig *config,
8485

8586
bool keeper_config_merge_options(KeeperConfig *config, KeeperConfig *options);
8687
bool keeper_config_update(KeeperConfig *config, int nodeId, int groupId);
87-
void keeper_config_destroy(KeeperConfig *config);
8888
bool keeper_config_update_with_absolute_pgdata(KeeperConfig *config);
8989

9090
#endif /* KEEPER_CONFIG_H */

src/bin/pg_autoctl/pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ typedef struct ReplicationSource
164164
char userName[NAMEDATALEN];
165165
char slotName[MAXCONNINFO];
166166
char password[MAXCONNINFO];
167-
char maximumBackupRate[MAXCONNINFO];
167+
char maximumBackupRate[MAXIMUM_BACKUP_RATE_LEN];
168168
char backupDir[MAXCONNINFO];
169169
char applicationName[MAXCONNINFO];
170170
char targetLSN[PG_LSN_MAXLENGTH];

src/bin/pg_autoctl/primary_standby.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,9 @@ standby_init_replication_source(LocalPostgresServer *postgres,
804804
}
805805

806806
strlcpy(upstream->slotName, slotName, MAXCONNINFO);
807-
strlcpy(upstream->maximumBackupRate, maximumBackupRate, MAXCONNINFO);
807+
strlcpy(upstream->maximumBackupRate,
808+
maximumBackupRate,
809+
MAXIMUM_BACKUP_RATE_LEN);
808810
strlcpy(upstream->backupDir, backupDirectory, MAXCONNINFO);
809811

810812
if (targetLSN != NULL)

0 commit comments

Comments
 (0)