[PATCH] lib/util/string_wrappers.h breaks when compiling with clang

Ralph Böhme rb at sernet.de
Wed Oct 15 04:13:52 MDT 2014


Hi all!

Me and Volker noticed that compiling Samba with clang (eg on FreeBSD10
or OS X) breaks here:

OS X:

[3544/3818] Linking default/source3/nmbd/nmbd
clang: warning: argument unused during compilation: '-pie'
Undefined symbols for architecture x86_64:
  "___unsafe_string_function_usage_here_size_t__", referenced from:
      _send_announcement in nmbd_sendannounce_120.o
      _send_lm_announcement in nmbd_sendannounce_120.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)

FreeBSD 10:

[3559/3706] Linking default/source3/nmbd/nmbd
default/source3/nmbd/nmbd_sendannounce_150.o: In function
`send_announcement':
/usr/home/ralph/samba/test/bin/../source3/nmbd/nmbd_sendannounce.c:123:
undefined reference to `__unsafe_string_function_usage_here_size_t__'
default/source3/nmbd/nmbd_sendannounce_150.o: In function
`send_lm_announcement':
/usr/home/ralph/samba/test/bin/../source3/nmbd/nmbd_sendannounce.c:152:
undefined reference to `__unsafe_string_function_usage_here_size_t__'
cc: error: linker command failed with exit code 1 (use -v to see
invocation)
Waf: Leaving directory `/usr/home/ralph/samba/test/bin'

It seems the waf configure check wasn't detecting clangs behaviour
correctly returning a false positive.

Attached patch fixes it for me, compilation passes on FreeBSD 10 with
clang and behaviour is unchanged on Linux.

Please review and push, if ok.

Thanks!
-Ralph

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de,mailto:kontakt@sernet.de
-------------- next part --------------
>From 97b3f77f822115e783b0bf251b76a7d3e31a234a Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Sat, 11 Oct 2014 20:50:46 +0200
Subject: [PATCH] build: lib/util/string_wrappers.h: fix optimisation check for
 clang

Building with clang resulted in an error with undefined symbols
___unsafe_string_function_usage_here_size_t__ etc. Turns out the
existing check whether the compiler optimizes out functions doesn't
match the use case, ie the check said yes, but the functions were not
optimized out.

Signed-off-by: Ralph Boehme <slow at samba.org>
---
 source3/wscript | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/source3/wscript b/source3/wscript
index 4f182bf..2083f01 100644
--- a/source3/wscript
+++ b/source3/wscript
@@ -349,12 +349,28 @@ int main(int argc, char **argv)
 
     # Check if the compiler will optimize out functions
     conf.CHECK_CODE('''
-if (0) {
-    this_function_does_not_exist();
-} else {
-    return 1;
+#include <sys/types.h>
+size_t __unsafe_string_function_usage_here_size_t__(void);
+#define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
+static size_t push_string_check_fn(void *dest, const char *src, size_t dest_len) {
+	return 0;
+}
+
+#define push_string_check(dest, src, dest_len) \
+    (CHECK_STRING_SIZE(dest, dest_len) \
+    ? __unsafe_string_function_usage_here_size_t__()	\
+    : push_string_check_fn(dest, src, dest_len))
+
+int main(int argc, char **argv) {
+    char outbuf[1024];
+    char *p = outbuf;
+    const char *foo = "bar";
+    p += 31 + push_string_check(p + 31, foo, sizeof(outbuf) - (p + 31 - outbuf));
+    return 0;
 }''', 'HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS',
-        msg="Checking if the compiler will optimize out functions")
+            addmain=False,
+            add_headers=False,
+            msg="Checking if the compiler will optimize out functions")
 
     # Check if the compiler supports the LL suffix on long long integers
     # AIX needs this
-- 
1.9.3



More information about the samba-technical mailing list