From daa4cd4177690641a8fc29b836824856abf60711 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: haxstring: Add parenthesis to macro arguments WRITES(fd, *s) where s is a pointer to a struct string will not compile, as the macro expands to write(fd, *s.data, *s.len), which it expects s to be a struct string and attempts to use s.data and s.len as pointers. This is, of course, erroneous. The correct expansion is write(fd, (*s).data, (*s).len); while write(fd, s->data, s->len) is desirable, it is not achievable with a simple macro expansion. In any case, the parenthesis shall be added. --- haxstring.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/haxstring.h b/haxstring.h index 0c72ff3..f0c3947 100644 --- a/haxstring.h +++ b/haxstring.h @@ -40,6 +40,6 @@ struct string { #define STRING(x) (struct string){x, sizeof(x)-1} #define NULSTR(x) (struct string){x, strlen(x)} -#define STRING_EQ(x, y) (x.len == y.len && memcmp(x.data, y.data, x.len) == 0) +#define STRING_EQ(x, y) ((x).len == (y).len && memcmp((x).data, (y).data, (x).len) == 0) -#define WRITES(x, y) write(x, y.data, y.len) +#define WRITES(x, y) write((x), (y).data, (y).len) -- cgit v1.2.3