From b57718fb7ed6f813f41b655ecbab1f6edd64ded0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 21 Apr 2009 11:14:50 -0700 Subject: http11: cleanup some CPP macros Use the "do {} while (0)" idiom to wrap the macros we can wrap to avoid having to worry about semi-colons when using them. Unfortunately DEF_MAX_LENGTH can't be wrapped with "do {} while (0)". But we do now correctly constify the strings DEF_MAX_LENGTH creates and also make them static to reduce visibility. --- ext/unicorn/http11/http11.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ext/unicorn/http11/http11.c b/ext/unicorn/http11/http11.c index 36e0e54..d37b557 100644 --- a/ext/unicorn/http11/http11.c +++ b/ext/unicorn/http11/http11.c @@ -54,14 +54,25 @@ static VALUE global_localhost; static VALUE global_http; /** Defines common length and error messages for input length validation. */ -#define DEF_MAX_LENGTH(N,length) const size_t MAX_##N##_LENGTH = length; const char *MAX_##N##_LENGTH_ERR = "HTTP element " # N " is longer than the " # length " allowed length." +#define DEF_MAX_LENGTH(N, length) \ + static const size_t MAX_##N##_LENGTH = length; \ + static const char * const MAX_##N##_LENGTH_ERR = \ + "HTTP element " # N " is longer than the " # length " allowed length." -/** Validates the max length of given input and throws an HttpParserError exception if over. */ -#define VALIDATE_MAX_LENGTH(len, N) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); } +/** + * Validates the max length of given input and throws an HttpParserError + * exception if over. + */ +#define VALIDATE_MAX_LENGTH(len, N) do { \ + if (len > MAX_##N##_LENGTH) \ + rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); \ +} while (0) /** Defines global strings in the init method. */ -#define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N) - +#define DEF_GLOBAL(N, val) do { \ + global_##N = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \ + rb_global_variable(&global_##N); \ +} while (0) /* Defines the maximum allowed lengths for various input elements.*/ DEF_MAX_LENGTH(FIELD_NAME, 256); -- cgit v1.2.3-24-ge0c7