]> granicus.if.org Git - libevent/commitdiff
Fix a major parenthesis bug in EVUTIL_UPCAST.
authorNick Mathewson <nickm@torproject.org>
Mon, 2 Nov 2009 19:30:25 +0000 (19:30 +0000)
committerNick Mathewson <nickm@torproject.org>
Mon, 2 Nov 2009 19:30:25 +0000 (19:30 +0000)
Fortunately, this didn't hurt anything previously, since we had no actual users of the macro where the offset of the base type wasn't 0.

svn:r1488

test/regress_util.c
util-internal.h

index 52a43913a80edc58bac0ae53a8f7f6d7f0842516..0ca2b06789f4ad69839cd93d4581638a9c064613 100644 (file)
@@ -465,6 +465,30 @@ end:
        ;
 }
 
+struct example_struct {
+       long a;
+       const char *b;
+       long c;
+};
+
+static void
+test_evutil_upcast(void *arg)
+{
+       struct example_struct es1;
+       const char **cp;
+       es1.a = 5;
+       es1.b = "Hello";
+       es1.c = -99;
+
+       tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(long));
+
+       cp = &es1.b;
+       tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
+
+end:
+       ;
+
+}
 
 struct testcase_t util_testcases[] = {
        { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
@@ -475,6 +499,7 @@ struct testcase_t util_testcases[] = {
        { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
        { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
        { "log", test_evutil_log, TT_FORK, NULL, NULL },
+       { "upcast", test_evutil_upcast, 0, NULL, NULL },
        END_OF_TESTCASES,
 };
 
index 853f22006a9aa544bba5ce28601a83dad85a53a3..dc1121166a84aa09e62f135aa6df5362c6c704e7 100644 (file)
@@ -130,7 +130,7 @@ extern const char EVUTIL_TOLOWER_TABLE[];
     }
  */
 #define EVUTIL_UPCAST(ptr, type, field)                                \
-       ((type *)((char*)ptr) - evutil_offsetof(type, field))
+       ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
 
 
 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);