Thread (29 messages) 29 messages, 4 authors, 2019-05-28
STALE2557d
Revisions (3)
  1. v1 [diff vs current]
  2. v1 current
  3. v1 [diff vs current]

[PATCH 01/16] lib: fix match_string() helper when array size is positive

From: Alexandru Ardelean <hidden>
Date: 2019-05-08 11:34:26
Also in: alsa-devel, cgroups, dri-devel, intel-gfx, kvm, linux-arm-kernel, linux-clk, linux-fbdev, linux-gpio, linux-ide, linux-integrity, linux-mm, linux-mmc, linux-omap, linux-pci, linux-pm, linux-rockchip, linux-security-module, linux-tegra, linux-usb, linuxppc-dev, lkml, netdev
Subsystem: generic string library, library code, the rest · Maintainers: Kees Cook, Andrew Morton, Linus Torvalds

The documentation the `_match_string()` helper mentions that `n`
(size of the given array) should be:
 * @n: number of strings in the array or -1 for NULL terminated arrays

The behavior of the function is different, in the sense that it exits on
the first NULL element in the array, regardless of whether `n` is -1 or a
positive number.

This patch changes the behavior, to exit the loop when a NULL element is
found and n == -1. Essentially, this aligns the behavior with the
doc-string.

There are currently many users of `match_string()`, and so, in order to go
through them, the next patches in the series will focus on doing some
cosmetic changes, which are aimed at grouping the users of
`match_string()`.

Signed-off-by: Alexandru Ardelean <redacted>
---
 lib/string.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lib/string.c b/lib/string.c
index 3ab861c1a857..76edb7bf76cb 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -648,8 +648,11 @@ int match_string(const char * const *array, size_t n, const char *string)
 
 	for (index = 0; index < n; index++) {
 		item = array[index];
-		if (!item)
+		if (!item) {
+			if (n != (size_t)-1)
+				continue;
 			break;
+		}
 		if (!strcmp(item, string))
 			return index;
 	}
-- 
2.17.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help