Skip to content

Commit 908814a

Browse files
zhhyu7jerpelea
authored andcommitted
libc/lib_bzero:Add bzero prototype.
Implement the bzero function as an alternative to macro expansion. and support gcc FORTIFY_SOURCE features for nuttx libc This function will use gcc's function __builtin_dynamic_object_size and __builtin_object_size Its function is to obtain the size of the object through compilation, so as to judge whether there are out-of-bounds operations in commonly used functions. It should be noted that the option -O2 and above is required to enable this function Signed-off-by: zhanghongyu <[email protected]>
1 parent 4ffad26 commit 908814a

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

include/strings.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
#define bcopy(b1,b2,len) memmove(b2,b1,len)
5050
#endif
5151

52-
#ifndef bzero /* See mm/README.txt */
53-
#define bzero(s,n) memset(s,0,n)
54-
#endif
55-
5652
#define strcasecmp_l(s1, s2, l) strcasecmp(s1, s2)
5753
#define strncasecmp_l(s1, s2, n, l) strncasecmp(s1, s2, n)
5854

@@ -156,11 +152,13 @@ FAR char *rindex(FAR const char *s, int c);
156152
int strcasecmp(FAR const char *, FAR const char *);
157153
int strncasecmp(FAR const char *, FAR const char *, size_t);
158154

155+
void bzero(FAR void *s, size_t n);
156+
159157
#if CONFIG_FORTIFY_SOURCE > 0
160158
fortify_function(bzero) void bzero(FAR void *s, size_t n)
161159
{
162160
fortify_assert(n <= fortify_size(s, 0));
163-
return bzero(s, n);
161+
return __real_bzero(s, n);
164162
}
165163
#endif
166164

libs/libc/string/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c lib_strrchr.c
3030
CSRCS += lib_strspn.c lib_strstr.c lib_strtok.c lib_strtokr.c
3131
CSRCS += lib_strsep.c lib_strerrorr.c lib_explicit_bzero.c lib_strsignal.c
3232
CSRCS += lib_index.c lib_rindex.c lib_timingsafe_bcmp.c lib_strverscmp.c
33-
CSRCS += lib_mempcpy.c lib_rawmemchr.c
33+
CSRCS += lib_mempcpy.c lib_rawmemchr.c lib_bzero.c
3434

3535
CSRCS += lib_memchr.c lib_memcmp.c lib_memmove.c lib_memset.c
3636
CSRCS += lib_strchr.c lib_strcmp.c lib_strcpy.c lib_strlcat.c

libs/libc/string/lib_bzero.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/****************************************************************************
2+
* libs/libc/string/lib_bzero.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <sys/types.h>
27+
#include <strings.h>
28+
29+
/****************************************************************************
30+
* Public Functions
31+
****************************************************************************/
32+
33+
/****************************************************************************
34+
* Name: bzero
35+
****************************************************************************/
36+
37+
void bzero(FAR void *s, size_t n)
38+
{
39+
memset(s, 0, n);
40+
}

0 commit comments

Comments
 (0)