This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 9123a4d3dc2b801718d22576ee472e2fc04c722e
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 27 18:44:25 2026 +0100

    libs/libc/string: Make bcmp(), bcopy() and bzero() functions.
    
    <strings.h> defined bcmp, bcopy and bzero as macros of memcmp, memmove
    and memset, and libc had no such functions.  A program that declares
    them itself, as older programs and generated headers do (vim's
    auto/osdef.h: "extern void bzero(void *, size_t);"), then did not
    compile, and a link test did not find them.
    
    Make them functions of libc.  GCC knows these names as built-in
    functions, so calls with a known size still become inline code.  With
    CONFIG_FORTIFY_SOURCE, bcopy() and bzero() get the same checks as
    memmove() and memset().
    
    An earlier bzero() function was reverted (cb720024b66): clang for macOS
    turns memset(s, 0, n) into a call of bzero(), so bzero() called itself
    in the simulator.  bzero() now has no_builtin("memset"), as memset()
    has: the call stays a call of memset().
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/strings.h               | 47 +++++++++++++++++++++++------------------
 libs/libc/libc.csv              |  3 +++
 libs/libc/string/CMakeLists.txt |  3 +++
 libs/libc/string/Make.defs      |  1 +
 libs/libc/string/lib_bcmp.c     | 45 +++++++++++++++++++++++++++++++++++++++
 libs/libc/string/lib_bcopy.c    | 45 +++++++++++++++++++++++++++++++++++++++
 libs/libc/string/lib_bzero.c    | 47 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 171 insertions(+), 20 deletions(-)

diff --git a/include/strings.h b/include/strings.h
index 81648360a3d..660b071df53 100644
--- a/include/strings.h
+++ b/include/strings.h
@@ -36,26 +36,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Compatibility definitions
- *
- * Marked LEGACY in Open Group Base Specifications Issue 6/
- * IEEE Std 1003.1-2004
- * Removed from Open Group Base Specifications Issue 7/
- * IEEE Std 1003.1-2008
- */
-
-#ifndef bcmp
-#define bcmp(b1,b2,len)  memcmp(b1,b2,(size_t)len)
-#endif
-
-#ifndef bcopy
-#define bcopy(b1,b2,len) memmove(b2,b1,len)
-#endif
-
-#ifndef bzero
-#define bzero(s,n)       memset(s,0,n)
-#endif
-
 #define strcasecmp_l(s1, s2, l)     strcasecmp(s1, s2)
 #define strncasecmp_l(s1, s2, n, l) strncasecmp(s1, s2, n)
 
@@ -72,6 +52,33 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
+/* Compatibility functions
+ *
+ * Marked LEGACY in Open Group Base Specifications Issue 6/
+ * IEEE Std 1003.1-2004
+ * Removed from Open Group Base Specifications Issue 7/
+ * IEEE Std 1003.1-2008
+ */
+
+int bcmp(FAR const void *b1, FAR const void *b2, size_t len);
+void bcopy(FAR const void *b1, FAR void *b2, size_t len);
+void bzero(FAR void *s, size_t n);
+
+#if CONFIG_FORTIFY_SOURCE > 0
+fortify_function(bcopy) void bcopy(FAR const void *b1, FAR void *b2,
+                                   size_t len)
+{
+  fortify_assert(len <= fortify_size(b1, 0) && len <= fortify_size(b2, 0));
+  __real_bcopy(b1, b2, len);
+}
+
+fortify_function(bzero) void bzero(FAR void *s, size_t n)
+{
+  fortify_assert(n <= fortify_size(s, 0));
+  __real_bzero(s, n);
+}
+#endif
+
 int ffs(int j);
 
 #ifdef CONFIG_HAVE_BUILTIN_FFS
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 16886e4e316..ba5ea18a8d0 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -30,10 +30,13 @@
 "b16cos","fixedmath.h","","b16_t","b16_t"
 "b16sin","fixedmath.h","","b16_t","b16_t"
 "basename","libgen.h","","FAR char *","FAR char *"
+"bcmp","strings.h","","int","FAR const void *","FAR const void *","size_t"
+"bcopy","strings.h","","void","FAR const void *","FAR void *","size_t"
 
"bind_textdomain_codeset","libintl.h","defined(CONFIG_LIBC_LOCALE_GETTEXT)","FAR
 char *","FAR const char *","FAR const char *"
 "bindtextdomain","libintl.h","defined(CONFIG_LIBC_LOCALE_GETTEXT)","FAR char 
*","FAR const char *","FAR const char *"
 "bsearch","stdlib.h","","FAR void *","FAR const void *","FAR const void 
*","size_t","size_t","FAR void *"
 "btowc","wchar.h","","wint_t","int"
+"bzero","strings.h","","void","FAR void *","size_t"
 "calloc","stdlib.h","","FAR void *","size_t","size_t"
 "cfgetspeed","termios.h","","speed_t","FAR const struct termios *"
 "cfsetspeed","termios.h","","int","FAR struct termios *","speed_t"
diff --git a/libs/libc/string/CMakeLists.txt b/libs/libc/string/CMakeLists.txt
index eb1f5494ee3..a9c83e70953 100644
--- a/libs/libc/string/CMakeLists.txt
+++ b/libs/libc/string/CMakeLists.txt
@@ -23,6 +23,9 @@
 # Add the string C files to the build
 
 set(SRCS
+    lib_bcmp.c
+    lib_bcopy.c
+    lib_bzero.c
     lib_ffs.c
     lib_ffsl.c
     lib_ffsll.c
diff --git a/libs/libc/string/Make.defs b/libs/libc/string/Make.defs
index d0c90d88d1b..790cc0b33d3 100644
--- a/libs/libc/string/Make.defs
+++ b/libs/libc/string/Make.defs
@@ -22,6 +22,7 @@
 
 # Add the string C files to the build
 
+CSRCS += lib_bcmp.c lib_bcopy.c lib_bzero.c
 CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c
 CSRCS += lib_flsll.c lib_isbasedigit.c lib_memmem.c
 CSRCS += lib_popcount.c lib_popcountl.c lib_popcountll.c
diff --git a/libs/libc/string/lib_bcmp.c b/libs/libc/string/lib_bcmp.c
new file mode 100644
index 00000000000..2b783c1744e
--- /dev/null
+++ b/libs/libc/string/lib_bcmp.c
@@ -0,0 +1,45 @@
+/****************************************************************************
+ * libs/libc/string/lib_bcmp.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <strings.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bcmp
+ *
+ * Description:
+ *   Compare len bytes of b1 and b2: zero when they are the same.
+ *
+ ****************************************************************************/
+
+int bcmp(FAR const void *b1, FAR const void *b2, size_t len)
+{
+  return memcmp(b1, b2, len);
+}
diff --git a/libs/libc/string/lib_bcopy.c b/libs/libc/string/lib_bcopy.c
new file mode 100644
index 00000000000..1233a1ceca2
--- /dev/null
+++ b/libs/libc/string/lib_bcopy.c
@@ -0,0 +1,45 @@
+/****************************************************************************
+ * libs/libc/string/lib_bcopy.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <strings.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bcopy
+ *
+ * Description:
+ *   Copy len bytes from b1 to b2.  The areas may overlap.
+ *
+ ****************************************************************************/
+
+void bcopy(FAR const void *b1, FAR void *b2, size_t len)
+{
+  memmove(b2, b1, len);
+}
diff --git a/libs/libc/string/lib_bzero.c b/libs/libc/string/lib_bzero.c
new file mode 100644
index 00000000000..6658ac3c827
--- /dev/null
+++ b/libs/libc/string/lib_bzero.c
@@ -0,0 +1,47 @@
+/****************************************************************************
+ * libs/libc/string/lib_bzero.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <strings.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bzero
+ *
+ * Description:
+ *   Set n bytes of s to zero.  no_builtin: clang for macOS turns
+ *   memset(s, 0, n) into a call of bzero(), an endless loop here.
+ *
+ ****************************************************************************/
+
+no_builtin("memset")
+void bzero(FAR void *s, size_t n)
+{
+  memset(s, 0, n);
+}

Reply via email to