From 826e457cb9f80ae4fa20d0563acf6ef22b1600de Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 13 Dec 2023 15:35:23 +0100 Subject: [PATCH] Change the semantic of `label_set_clone`. It checks to ensure that `dest` is empty and returns `EINVAL` otherwise. --- src/daemon/metric.c | 4 ++++ src/daemon/metric.h | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/daemon/metric.c b/src/daemon/metric.c index 55edc7a7d..d273863d7 100644 --- a/src/daemon/metric.c +++ b/src/daemon/metric.c @@ -214,6 +214,10 @@ void label_set_reset(label_set_t *labels) { } int label_set_clone(label_set_t *dest, label_set_t src) { + if (dest == NULL || dest->num != 0) { + return EINVAL; + } + if (src.num == 0) { return 0; } diff --git a/src/daemon/metric.h b/src/daemon/metric.h index a91f596f1..35fdf6dad 100644 --- a/src/daemon/metric.h +++ b/src/daemon/metric.h @@ -70,9 +70,9 @@ typedef struct { size_t num; } label_set_t; -/* label_set_clone copies all the labels in src into dest. If dest contains - * any labels prior to calling label_set_clone, the associated memory is - * leaked. */ +/* label_set_clone copies all the labels in src into dest. dest must be an empty + * label set, i.e. it must not contain any prior labels, otherwise EINVAL is + * returned. */ int label_set_clone(label_set_t *dest, label_set_t src); /* label_set_add adds a label to the label set. If a label with name already -- 2.47.2