This is the mail archive of the
mailing list for the libstdc++ project.
Re: Passing 0 for number of bytes to be scanned in memchr
- From: Aditya K <hiraditya at msn dot com>
- To: Jonathan Wakely <jwakely at redhat dot com>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 16 Feb 2017 12:40:02 +0000
- Subject: Re: Passing 0 for number of bytes to be scanned in memchr
- Authentication-results: sourceware.org; auth=none
- Authentication-results: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=msn.com;
- References: <KL1PR0601MB2021B092943830C1BB1221F0B65A0@KL1PR0601MB2021.apcprd06.prod.outlook.com>,<20170216121259.GS4602@redhat.com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
> If __s is a null pointer then passing it to memchr is undefined.
That means we are missing the check when __s == nullptr?
I also wanted to know what happens when __n is zero in the case of memchr. I think memchr returns null in this case so we can remove that check.
From: Jonathan Wakely <firstname.lastname@example.org>
Sent: Thursday, February 16, 2017 6:12 AM
To: Aditya K
Subject: Re: Passing 0 for number of bytes to be scanned in memchr
On 16/02/17 11:38 +0000, Aditya K wrote:
>I see that in char_traits<char>::find, we check if the number of bytes to be scanned is zero. I think memchr handles that case already but I do not have
>any reference to point out to. The documentation does not explicitly mention what happens when 'n' is zero. If n==0 is already handled then
>we can safely remove this check. Please let me know your thoughts.
>269 static const char_type*
>270 find(const char_type* __s, size_t __n, const char_type& __a)
>272 if (__n == 0)
>273 return 0;
>274 return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
If __s is a null pointer then passing it to memchr is undefined.
I'm not 100% sure, but I don't think basic_string::find has the same
restriction, so we need to handle the case where it's null and avoid