]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cache: Reject duplicate cache names
authorTim Duesterhus <tim@bastelstu.be>
Tue, 18 Aug 2020 20:20:27 +0000 (22:20 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 18 Aug 2020 20:51:24 +0000 (22:51 +0200)
Using a duplicate cache name most likely is the result of a misgenerated
configuration. There is no good reason to allow this, as the duplicate
caches can't be referred to.

This commit resolves GitHub issue #820.

It can be argued whether this is a fix for a bug or not. I'm erring on the
side of caution and marking this as a "new feature". It can be considered for
backporting to 2.2, but for other branches the risk of accidentally breaking
some working (but non-ideal) configuration might be too large.

src/cache.c

index 4f8d2e04a0a55b6411244a6b2040c2ef9e0558f8..b42262948a80904d31d08d24b2171b50f7a932e4 100644 (file)
@@ -1176,7 +1176,7 @@ int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
        if (strcmp(args[0], "cache") == 0) { /* new cache section */
 
                if (!*args[1]) {
-                       ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
+                       ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
                                 file, linenum, args[0]);
                        err_code |= ERR_ALERT | ERR_ABORT;
                        goto out;
@@ -1188,6 +1188,8 @@ int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
                }
 
                if (tmp_cache_config == NULL) {
+                       struct cache *cache_config;
+
                        tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
                        if (!tmp_cache_config) {
                                ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
@@ -1197,10 +1199,20 @@ int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
 
                        strlcpy2(tmp_cache_config->id, args[1], 33);
                        if (strlen(args[1]) > 32) {
-                               ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
+                               ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
                                           file, linenum, tmp_cache_config->id);
                                err_code |= ERR_WARN;
                        }
+
+                       list_for_each_entry(cache_config, &caches_config, list) {
+                               if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
+                                       ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
+                                                file, linenum, tmp_cache_config->id);
+                                       err_code |= ERR_ALERT | ERR_ABORT;
+                                       goto out;
+                               }
+                       }
+
                        tmp_cache_config->maxage = 60;
                        tmp_cache_config->maxblocks = 0;
                        tmp_cache_config->maxobjsz = 0;