From acf2744ed79eb81f2ceba2baa3d0e5861ea9ad53 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Thu, 8 Jan 2015 17:19:02 +0900 Subject: [PATCH] Replace magic number with defined constant And use it to restrict the number of rows retrieved to avoid overflowing the array. --- repmgr.h | 2 ++ repmgrd.c | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/repmgr.h b/repmgr.h index 797f61a2..1f8d6b74 100644 --- a/repmgr.h +++ b/repmgr.h @@ -46,6 +46,8 @@ #define DEFAULT_DBNAME "postgres" #define DEFAULT_REPMGR_SCHEMA_PREFIX "repmgr_" +#define FAILOVER_NODES_MAX_CHECK 50 + #define MANUAL_FAILOVER 0 #define AUTOMATIC_FAILOVER 1 diff --git a/repmgrd.c b/repmgrd.c index 8f05f277..16d17d2d 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -275,7 +275,7 @@ main(int argc, char **argv) /* - * MAIN LOOP This loops cicles once per failover and at startup + * MAIN LOOP This loops cycles at startup and once per failover and * Requisites: - my_local_conn needs to be already setted with an active * connection - no master connection */ @@ -782,7 +782,7 @@ do_failover(void) * will get info about until 50 nodes, which seems to be large enough for * most scenarios */ - t_node_info nodes[50]; + t_node_info nodes[FAILOVER_NODES_MAX_CHECK]; /* initialize to keep compiler quiet */ t_node_info best_candidate = {-1, "", InvalidXLogRecPtr, false, false, false}; @@ -791,8 +791,8 @@ do_failover(void) sprintf(sqlquery, "SELECT id, conninfo, witness " " FROM %s.repl_nodes " " WHERE cluster = '%s' " - " ORDER BY priority, id ", - repmgr_schema, local_options.cluster_name); + " ORDER BY priority, id LIMIT %i", + repmgr_schema, local_options.cluster_name, FAILOVER_NODES_MAX_CHECK); res = PQexec(my_local_conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK)