strnlen, strict ansi, newlib vs glibc
Joel Sherrill
joel.sherrill@oarcorp.com
Thu Aug 14 22:53:00 GMT 2014
This type of change is always tricky. FreeBSD's string.h only had
__STRICT_ANSI__. I read the glibc version and attempted to
translate their feature flags into the BSD style ones we have
from <sys/cdefs.h>.
There are a few methods I have no idea how to guard:
+ The block of Windows strcmpi and friends at the bottom
of the file.
+ strlwr() and strupr(). No idea where those originated.
+ _strerror_r() is another unknown origination.
Attached is try1. I expect there to be a few rounds of this.
I did build a complete sparc-rtems toolset and all RTEMS tests.
Please check what I did and offer corrections and improvements.
I tried to not rearrange the file. There is likely opportunity to
group methods with similar guards.
--joel
On 8/14/2014 10:57 AM, Joel Sherrill wrote:
>
> On August 14, 2014 10:30:55 AM CDT, Luca Barbato <lu_zero@gentoo.org> wrote:
>> On 14/08/14 17:27, Joel Sherrill wrote:
>>> Hi
>>>
>>> I have some native C++ code that I developed on CentOS and it
>>> has no warnings. Someone moved it to Cygwin and reported warnings
>>> for strnlen() not being prototyped. I investigated and the program
>> did
>>> include string.h. I tried RTEMS tools and got the same as Cygwin
>>> since we have the same string.h from newlib.
>>>
>>> Investigating this, it appears that strnlen() is protected by
>>> __STRICT_ANSI__ on newlib and __USE_XOPEN2K8 on Linux.
>>>
>>> Command: g++ -Wall -std=c++0x -c strtest.cc
>>>
>>> This program is enough to reproduce the issue:
>>>
>>> #include <string.h>
>>>
>>> // size_t strnlen( const char *, size_t );
>>>
>>> size_t f( const char *str )
>>> {
>>> return strnlen( str, 1000 );
>>> }
>>>
>>>
>>> What do you all think?
>>>
>> It is a known issue with newlib headers.
>>
>> They mistakenly assume __STRICT_ANSI__ as nothing but old-C.
>>
>> using -std=c99 triggers the same kind of issues.
>>
>> The common way to solve it is adding -U__STRICT_ANSI__
> Thanks for the quick reply.
>
> How about I try to fix this based on what glibc and FreeBSD do?
>
>> lu
--
Joel Sherrill, Ph.D. Director of Research & Development
joel.sherrill@OARcorp.com On-Line Applications Research
Ask me about RTEMS: a free RTOS Huntsville AL 35805
Support Available (256) 722-9985
-------------- next part --------------
Index: newlib/libc/include/string.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/string.h,v
retrieving revision 1.35
diff -u -r1.35 string.h
--- newlib/libc/include/string.h 23 Jul 2013 07:05:30 -0000 1.35
+++ newlib/libc/include/string.h 14 Aug 2014 22:34:59 -0000
@@ -38,75 +38,109 @@
char *_EXFUN(strrchr,(const char *, int));
size_t _EXFUN(strspn,(const char *, const char *));
char *_EXFUN(strstr,(const char *, const char *));
-
#ifndef _REENT_ONLY
char *_EXFUN(strtok,(char *__restrict, const char *__restrict));
#endif
-
size_t _EXFUN(strxfrm,(char *__restrict, const char *__restrict, size_t));
-#ifndef __STRICT_ANSI__
+#if __POSIX_VISIBLE
char *_EXFUN(strtok_r,(char *__restrict, const char *__restrict, char **__restrict));
-
+#endif
+#if __BSD_VISIBLE
int _EXFUN(bcmp,(const void *, const void *, size_t));
void _EXFUN(bcopy,(const void *, void *, size_t));
void _EXFUN(bzero,(void *, size_t));
int _EXFUN(ffs,(int));
char *_EXFUN(index,(const char *, int));
+#endif
+#if __BSD_VISIBLE) || __XSI_VISIBLE
_PTR _EXFUN(memccpy,(_PTR __restrict, const _PTR __restrict, int, size_t));
+#endif
+#if __GNU_VISIBLE
_PTR _EXFUN(mempcpy,(_PTR, const _PTR, size_t));
_PTR _EXFUN(memmem, (const _PTR, size_t, const _PTR, size_t));
+#endi
_PTR _EXFUN(memrchr,(const _PTR, int, size_t));
+#if __GNU_VISIBLE
_PTR _EXFUN(rawmemchr,(const _PTR, int));
+#endif
+#if __BSD_VISIBLE
char *_EXFUN(rindex,(const char *, int));
+#endif
char *_EXFUN(stpcpy,(char *__restrict, const char *__restrict));
char *_EXFUN(stpncpy,(char *__restrict, const char *__restrict, size_t));
+#if __BSD_VISIBLE
int _EXFUN(strcasecmp,(const char *, const char *));
+#endif
+#if __GNU_VISIBLE
char *_EXFUN(strcasestr,(const char *, const char *));
char *_EXFUN(strchrnul,(const char *, int));
#endif
-#if !defined(__STRICT_ANSI__) || (_XOPEN_SOURCE - 0) >= 500
+#if (_XSI_VISIBLE - 0) >= 500
char *_EXFUN(strdup,(const char *));
#endif
#ifndef __STRICT_ANSI__
char *_EXFUN(_strdup_r,(struct _reent *, const char *));
#endif
-#if !defined(__STRICT_ANSI__) || (_XOPEN_SOURCE - 0) >= 700
+#if (_XSI_VISIBLE - 0) >= 700
char *_EXFUN(strndup,(const char *, size_t));
#endif
+
#ifndef __STRICT_ANSI__
char *_EXFUN(_strndup_r,(struct _reent *, const char *, size_t));
+#endif
+
/* There are two common strerror_r variants. If you request
_GNU_SOURCE, you get the GNU version; otherwise you get the POSIX
version. POSIX requires that #undef strerror_r will still let you
invoke the underlying function, but that requires gcc support. */
-#ifdef _GNU_SOURCE
-char *_EXFUN(strerror_r,(int, char *, size_t));
+#if __GNU_VISIBLE
+char *_EXFUN(strerror_r,(int, char *, size_t));
#else
# ifdef __GNUC__
-int _EXFUN(strerror_r,(int, char *, size_t)) __asm__ (__ASMNAME ("__xpg_strerror_r"));
+int _EXFUN(strerror_r,(int, char *, size_t))
+ __asm__ (__ASMNAME ("__xpg_strerror_r"));
# else
-int _EXFUN(__xpg_strerror_r,(int, char *, size_t));
+int _EXFUN(__xpg_strerror_r,(int, char *, size_t));
# define strerror_r __xpg_strerror_r
# endif
#endif
-size_t _EXFUN(strlcat,(char *, const char *, size_t));
-size_t _EXFUN(strlcpy,(char *, const char *, size_t));
-int _EXFUN(strncasecmp,(const char *, const char *, size_t));
+
+/* Reentrant version of strerror. */
+/* XXX Visibility conditional is unknown to me. --joel */
+char * _EXFUN(_strerror_r, (struct _reent *, int, int, int *));
+
+#if __BSD_VISIBLE
+size_t _EXFUN(strlcat,(char *, const char *, size_t));
+size_t _EXFUN(strlcpy,(char *, const char *, size_t));
+#endif
+#if __BSD_VISIBLE || __POSIX_VISIBLE
+int _EXFUN(strncasecmp,(const char *, const char *, size_t));
+#endif
+#if !defined(__STRICT_ANSI__) || (_XOPEN_SOURCE - 0) >= 500
size_t _EXFUN(strnlen,(const char *, size_t));
+#endif
+#if __BSD_VISIBLE
char *_EXFUN(strsep,(char **, const char *));
+#endif
+
+/*
+ * The origin of these is unknown to me so I am conditionalizing them
+ * on __STRICT_ANSI__. Finetuning this is definitely needed. --joel
+ */
+#if !defined(__STRICT_ANSI__)
char *_EXFUN(strlwr,(char *));
char *_EXFUN(strupr,(char *));
+#endif
+
#ifndef DEFS_H /* Kludge to work around problem compiling in gdb */
-char *_EXFUN(strsignal, (int __signo));
+char *_EXFUN(strsignal, (int __signo));
#endif
+
#ifdef __CYGWIN__
-int _EXFUN(strtosigno, (const char *__name));
+int _EXFUN(strtosigno, (const char *__name));
#endif
-/* Recursive version of strerror. */
-char * _EXFUN(_strerror_r, (struct _reent *, int, int, int *));
-
#if defined _GNU_SOURCE && defined __GNUC__
#define strdupa(__s) \
(__extension__ ({const char *__in = (__s); \
@@ -135,8 +169,6 @@
#define strnicmp strncasecmp
#endif
-#endif /* ! __STRICT_ANSI__ */
-
#include <sys/string.h>
_END_STD_C
More information about the Newlib
mailing list