]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
internal: Add STRLIM macro for checking string length using strnlen()
authorPeter Krempa <pkrempa@redhat.com>
Wed, 24 Nov 2021 16:03:07 +0000 (17:03 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 1 Dec 2021 12:39:47 +0000 (13:39 +0100)
As a microoprimization when checking whether length of a string fits
into a limit we don't necessarily need to calculate the full length but
can use strnlen to check only LIMIT+1 chars. Add a macro which will
simplify the expressions.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/internal.h

index d3809bf057aa1c1bec66616c326d5c7d57cc712f..2e404cd705d6fd0562fcb8bac02f2e8f74a3eab2 100644 (file)
 #define STRPREFIX(a, b) (strncmp(a, b, strlen(b)) == 0)
 #define STRCASEPREFIX(a, b) (g_ascii_strncasecmp(a, b, strlen(b)) == 0)
 #define STRSKIP(a, b) (STRPREFIX(a, b) ? (a) + strlen(b) : NULL)
+/**
+ * STRLIM
+ * @str: pointer to a string (evaluated once)
+ * @lim: length limit (evaluated twice)
+ *
+ * Evaluates as true if length of @str doesn't exceed the limit @lim. Note
+ * that @lim + 1 characters may be accessed.
+ */
+#define STRLIM(str, lim) (strnlen((str), (lim) + 1) <= (lim))
 
 #define STREQ_NULLABLE(a, b) (g_strcmp0(a, b) == 0)
 #define STRNEQ_NULLABLE(a, b) (g_strcmp0(a, b) != 0)