From: Nick Mathewson Date: Tue, 26 Jun 2012 14:55:23 +0000 (-0400) Subject: Fix a warning when using glibc's strcspn with clang. X-Git-Tag: tor-0.2.3.18-rc~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5fad3dc36b3aad7f2ea13e56d838c3eb37fb029d;p=thirdparty%2Ftor.git Fix a warning when using glibc's strcspn with clang. With glibc 2.15 and clang 3.0, I get warnings from where we use the strcpsn implementation in the header as strcspn(string, "="). This is apparently because clang sees that part of the strcspn macro expands to "="[2], and doesn't realize that that part of the macro is only evaluated when "="[1] != 0. --- diff --git a/changes/clang_strcspn b/changes/clang_strcspn new file mode 100644 index 0000000000..38670ac376 --- /dev/null +++ b/changes/clang_strcspn @@ -0,0 +1,3 @@ + o Minor bugfixes: + - Avoid a warning caused by using strcspn from glibc with clang 3.0. + Bugfix on 0.2.3.13-alpha. diff --git a/src/common/util.c b/src/common/util.c index d94dcf2003..51d932146d 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -3887,13 +3887,26 @@ tor_get_exit_code(const process_handle_t *process_handle, return PROCESS_EXIT_EXITED; } +/** Helper: return the number of characters in s preceding the first + * occurence of ch. If ch does not occur in s, return + * the length of s. Should be equivalent to strspn(s, "ch"). */ +static INLINE size_t +str_num_before(const char *s, char ch) +{ + const char *cp = strchr(s, ch); + if (cp) + return cp - s; + else + return strlen(s); +} + /** Return non-zero iff getenv would consider s1 and s2 * to have the same name as strings in a process's environment. */ int environment_variable_names_equal(const char *s1, const char *s2) { - size_t s1_name_len = strcspn(s1, "="); - size_t s2_name_len = strcspn(s2, "="); + size_t s1_name_len = str_num_before(s1, '='); + size_t s2_name_len = str_num_before(s2, '='); return (s1_name_len == s2_name_len && tor_memeq(s1, s2, s1_name_len)); @@ -3968,7 +3981,7 @@ process_environment_make(struct smartlist_t *env_vars) for (i = 0; i < n_env_vars; ++i) { const char *s = smartlist_get(env_vars_sorted, i); size_t slen = strlen(s); - size_t s_name_len = strcspn(s, "="); + size_t s_name_len = str_num_before(s, '='); if (s_name_len == slen) { log_warn(LD_GENERAL,