]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: pattern: Do not pass len = 0 to calloc()
authorTim Duesterhus <tim@bastelstu.be>
Tue, 17 Mar 2020 20:08:24 +0000 (21:08 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 18 Mar 2020 04:17:28 +0000 (05:17 +0100)
The behavior of calloc() when being passed `0` as `nelem` is implementation
defined. It may return a NULL pointer.

Avoid this issue by checking before allocating. While doing so adjust the local
integer variables that are used to refer to memory offsets to `size_t`.

This issue was introced in commit f91ac19299fe216a793ba6550dca06b688b31549. This
patch should be backported together with that commit.

src/pattern.c

index 3ea1f33d44c03b40894450b53755488281d17bae..a9425b047c01cfc56b715dd04e12818c21869853 100644 (file)
@@ -2660,10 +2660,10 @@ static int cmp_pat_ref(const void *_a, const void *_b)
  */
 int pattern_finalize_config(void)
 {
-       int len = 0;
-       int unassigned_pos = 0;
+       size_t len = 0;
+       size_t unassigned_pos = 0;
        int next_unique_id = 0;
-       int i, j;
+       size_t i, j;
        struct pat_ref *ref, **arr;
        struct list pr = LIST_HEAD_INIT(pr);
 
@@ -2676,6 +2676,10 @@ int pattern_finalize_config(void)
                        unassigned_pos++;
        }
 
+       if (len == 0) {
+               return 0;
+       }
+
        arr = calloc(len, sizeof(*arr));
        if (arr == NULL) {
                ha_alert("Out of memory error.\n");