s3-safe_string: Add checked_strlcpy()
authorAndrew Bartlett <abartlet@samba.org>
Tue, 22 Mar 2011 10:03:59 +0000 (21:03 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 23 Mar 2011 02:39:35 +0000 (03:39 +0100)
This is strlcpy, just with an extra check of the parameters with
sizeof(), use only where that works.

Andrew Bartlett

Autobuild-User: Andrew Bartlett <abartlet@samba.org>
Autobuild-Date: Wed Mar 23 03:39:35 CET 2011 on sn-devel-104

source3/include/safe_string.h
source3/smbd/negprot.c

index a4648b57d44e8a44bf090aa89b4df6f4afe7d1b3..611f850d12384ecdef691a1fcb688d097c91c015 100644 (file)
@@ -144,6 +144,17 @@ size_t __unsafe_string_function_usage_here_char__(void);
     ? __unsafe_string_function_usage_here_size_t__() \
     : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags))
 
+/* This allows the developer to choose to check the arguments to
+   strlcpy.  if the compiler will optimize out function calls, then
+   use this to tell if we are have the correct size buffer (this works only
+   where sizeof() returns the size of the buffer, not the size of the
+   pointer), so stack and static variables only */
+
+#define checked_strlcpy(dest, src, size) \
+    (sizeof(dest) != (size) \
+    ? __unsafe_string_function_usage_here_size_t__() \
+     : strlcpy(dest, src, size))
+
 #else
 
 #define safe_strcpy safe_strcpy_fn
@@ -153,6 +164,7 @@ size_t __unsafe_string_function_usage_here_char__(void);
 #define clistr_push clistr_push_fn
 #define clistr_pull clistr_pull_fn
 #define srvstr_push srvstr_push_fn
+#define checked_strlcpy strlcpy
 
 #endif
 
index eb6dfa35a9c3de239a0e3b66eed1eec356aae0f7..f128396173f83f08d6f2d96cd57ad3648f2396e0 100644 (file)
@@ -238,7 +238,7 @@ DATA_BLOB negprot_spnego(TALLOC_CTX *ctx, struct smbd_server_connection *sconn)
 
        memset(blob_out.data, '\0', 16);
 
-       safe_strcpy(unix_name, global_myname(), sizeof(unix_name)-1);
+       checked_strlcpy(unix_name, global_myname(), sizeof(unix_name));
        strlower_m(unix_name);
        push_ascii_nstring(dos_name, unix_name);
        safe_strcpy((char *)blob_out.data, dos_name, 16);