[PATCH 3/6] internal: Add STRLIM macro for checking string length using strnlen()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



As a microoprimization when checking whether length of a string fits
into a limit we don't necessarily need to calculate the full lenght but
can use strnlen to check only LIMIT+1 chars. Add a macro which will
simplify the expressions.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/internal.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/internal.h b/src/internal.h
index d3809bf057..2e404cd705 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -87,6 +87,15 @@
 #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)
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux