Re: Integer overflows in memchr

2024-07-14 Thread Po Lu
Paul Eggert writes: > On 6/30/24 13:14, Po Lu wrote: >> I think there should be a trivial test for a functional strnlen in >> strnlen.m4, since it would be terrible to duplicate what ought to be the >> responsibility of Gnulib in Emacs's configure.ac. > > Here's a first cut at doing that, as a pa

Re: Integer overflows in memchr

2024-07-12 Thread Paul Eggert
On 6/30/24 13:14, Po Lu wrote: I think there should be a trivial test for a functional strnlen in strnlen.m4, since it would be terrible to duplicate what ought to be the responsibility of Gnulib in Emacs's configure.ac. Here's a first cut at doing that, as a patch to Emacs master that I have

Re: Integer overflows in memchr

2024-07-11 Thread Paul Eggert
On 7/11/24 17:50, Eric Blake wrote: In contrast, it's not OK to call memchr ("", 0, SIZE_MAX). ...this claim appears to be unsupported. Both C23 and POSIX 2024 state that memchr() "...shall behave as if it reads the characters sequentially and stops as soon as a matching character is found",

Re: Integer overflows in memchr

2024-07-11 Thread Eric Blake
On Wed, Jun 26, 2024 at 05:33:55PM GMT, Paul Eggert wrote: > On 6/26/24 07:57, Bruno Haible wrote: > > Po Lu wrote: > > > I believe that the semantics of the POSIX specification of this GNU > > > function omit the implied guarantee that strnlen will never examine > > > bytes beyond the first null b

Re: Integer overflows in memchr

2024-07-09 Thread Eric Blake
On Wed, Jul 10, 2024 at 01:55:51AM GMT, Paul Eggert wrote: > On 7/9/24 22:03, Eric Blake wrote: > > https://www.austingroupbugs.net/view.php?id=1834#c6830 > > > > The current draft of proposed wording would have the C standard state: > > > > 2 The strnlen function counts not more than n character

Re: Integer overflows in memchr

2024-07-09 Thread Jeffrey Walton
On Tue, Jul 9, 2024 at 7:56 PM Paul Eggert wrote: > > On 7/9/24 22:03, Eric Blake wrote: > > https://www.austingroupbugs.net/view.php?id=1834#c6830 > > > > The current draft of proposed wording would have the C standard state: > > > > 2 The strnlen function counts not more than n characters (a nul

Re: Integer overflows in memchr

2024-07-09 Thread Paul Eggert
On 7/9/24 22:03, Eric Blake wrote: https://www.austingroupbugs.net/view.php?id=1834#c6830 The current draft of proposed wording would have the C standard state: 2 The strnlen function counts not more than n characters (a null character and characters that follow it are not counted) in the array

Re: Integer overflows in memchr

2024-07-09 Thread Eric Blake
On Wed, Jun 26, 2024 at 05:33:55PM GMT, Paul Eggert wrote: > On 6/26/24 07:57, Bruno Haible wrote: > > Po Lu wrote: > > > I believe that the semantics of the POSIX specification of this GNU > > > function omit the implied guarantee that strnlen will never examine > > > bytes beyond the first null b

Re: Integer overflows in memchr

2024-07-01 Thread Paul Eggert
On 7/1/24 02:16, Po Lu wrote: but Gnulib (and by extension Emacs) does not invoke the Autoconf test: AC_DEFUN([gl_FUNC_STRNLEN], [ AC_REQUIRE([gl_STRING_H_DEFAULTS]) dnl Persuade glibc to declare strnlen(). AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) AC_CHECK_DECLS_ONCE([strnlen]) <--

Re: Integer overflows in memchr

2024-06-30 Thread Po Lu
Paul Eggert writes: > Autoconf's existing AC_FUNC_STRNLEN uses AC_RUN_IFELSE so this > wouldn't work as-is. > > Perhaps you could write up a patch against Autoconf? That wouldn't > count against the Gnulib quota Sure, but Gnulib (and by extension Emacs) does not invoke the Autoconf test: A

Re: Integer overflows in memchr

2024-06-30 Thread Paul Eggert
On 6/30/24 13:18, Po Lu wrote: How many lines remain of my 15-line quota of contributions to Gnulib? I wouldn't worry about the quota; we can solve that problem later as need be. #if defined __ANDROID_API__ && (__ANDROID_API__ < 21) you lose #endif /* defined __ANDROID_API__ && (__ANDROID

Re: Integer overflows in memchr

2024-06-30 Thread Po Lu
Paul Eggert writes: > Please feel free to write that. It's not something that would be easy > for me to do, as I don't know Android. Also, I don't know that it'd be > that trivial, as it might involve replacing the AC_FUNC_STRNLEN macro > of Autoconf (and we should propagate that fix into Autocon

Re: Integer overflows in memchr

2024-06-30 Thread Paul Eggert
On 6/30/24 12:14, Po Lu wrote: I think there should be a trivial test for a functional strnlen in strnlen.m4 Please feel free to write that. It's not something that would be easy for me to do, as I don't know Android. Also, I don't know that it'd be that trivial, as it might involve replacing

Re: Integer overflows in memchr

2024-06-30 Thread Po Lu
Paul Eggert writes: > On 6/27/24 01:55, Po Lu wrote: >> Google continue to support SDKs from 19 onwards in the standard support >> library: >> >> https://android-developers.googleblog.com/2023/10/androidx-minsdkversion-19.html >> which is the real threshold at which official support ends. > >

Re: Integer overflows in memchr

2024-06-30 Thread Po Lu
Paul Eggert writes: > On 6/27/24 02:37, Po Lu wrote: > >> Btw, doesn't this also mean that lib/strnlen.c (in Emacs, at least) is >> incorrect? > > Thanks for pointing that out. I installed the attached into Gnulib and > propagated it into Emacs master. Although I don't know of any > practical, su

Re: Integer overflows in memchr

2024-06-30 Thread Paul Eggert
On 6/27/24 01:55, Po Lu wrote: Google continue to support SDKs from 19 onwards in the standard support library: https://android-developers.googleblog.com/2023/10/androidx-minsdkversion-19.html which is the real threshold at which official support ends. Thanks, I don't know all the ins and

Re: Integer overflows in memchr

2024-06-30 Thread Paul Eggert
On 6/27/24 02:37, Po Lu wrote: Btw, doesn't this also mean that lib/strnlen.c (in Emacs, at least) is incorrect? Thanks for pointing that out. I installed the attached into Gnulib and propagated it into Emacs master. Although I don't know of any practical, supported platform where this matte

Re: Integer overflows in memchr

2024-06-29 Thread Po Lu
Po Lu writes: > Btw, doesn't this also mean that lib/strnlen.c (in Emacs, at least) is > incorrect? > > size_t > strnlen (const char *string, size_t maxlen) > { > const char *end = memchr (string, '\0', maxlen); > return end ? (size_t) (end - string) : maxlen; > } > > is virtually identical t

Re: Integer overflows in memchr

2024-06-26 Thread Po Lu
Paul Eggert writes: > On 6/26/24 12:33, Paul Eggert wrote: >> I installed the attached patch to document the Android 5.0 glitch > > Oops, attached the wrong patch. Here's the patch I meant to attach. Btw, doesn't this also mean that lib/strnlen.c (in Emacs, at least) is incorrect? size_t strnle

Re: Integer overflows in memchr

2024-06-26 Thread Po Lu
Paul Eggert writes: > Actually it's not incorrect because Android's memchr no longer has > undefined behavior with calls like memchr ("", 0, SIZE_MAX); it > reliably returns its first argument in that particular > example. Although this goes beyond what C and POSIX require for > memchr, it's OK f

Re: Integer overflows in memchr

2024-06-26 Thread Paul Eggert
On 6/26/24 12:33, Paul Eggert wrote: I installed the attached patch to document the Android 5.0 glitch Oops, attached the wrong patch. Here's the patch I meant to attach.From a03728ed45b8fae66fb30aa808dde76e4d57d865 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 26 Jun 2024 17:16:06 +01

Re: Integer overflows in memchr

2024-06-26 Thread Paul Eggert
On 6/26/24 07:57, Bruno Haible wrote: Po Lu wrote: I believe that the semantics of the POSIX specification of this GNU function omit the implied guarantee that strnlen will never examine bytes beyond the first null byte There is no such guarantee, not even implied. There seems to be some con

Re: Integer overflows in memchr

2024-06-26 Thread Po Lu
Bruno Haible writes: > When the text says "the array S of size MAXLEN", it means that the bytes > S[0], S[1], ..., S[MAXLEN-1] must be accessible. Which is not the case if > you pass MAXLEN as > ~(uintptr_t)S. > > The implementation could, for example, examine > S[0], S[MAXLEN-1], S[1], S[MAXLE

Re: Integer overflows in memchr

2024-06-26 Thread Bruno Haible
Po Lu wrote: > I believe that the semantics of the POSIX specification of this GNU > function omit the implied guarantee that strnlen will never examine > bytes beyond the first null byte There is no such guarantee, not even implied. > , made in (libc)String Length: > > If the array S of si

Re: Integer overflows in memchr

2024-06-26 Thread Po Lu
Bruno Haible writes: > This memchr invocation is wrong. memchr is defined in ISO C23 § 7.26.5.2 > "The memchr generic function locates the first occurrence of c (...) >in the initial n characters (...) of the object pointed to by s." > The term "object" being defined in § 3.15: > "region

Re: Integer overflows in memchr

2024-06-26 Thread Bruno Haible
Hi, Po Lu wrote: > It has been brought to my attention that a defective memchr is > distributed with certain Android E-book readers with Android 2.3.5 that > is liable to integer overflows with large values of N: > > memchr ("", 0, SIZE_MAX); This memchr invocation is wrong. memchr is defined

Re: Integer overflows in memchr

2024-06-25 Thread Po Lu
Po Lu writes: > disassembly of libc.so samples extracted from real Android devices > reveals that defective versions of memchr are present until Android 4.3, > API level 18. Correction, up to API level 22 on architectures without specific implementations of memrchr, as these issues suggest: h

Integer overflows in memchr

2024-06-25 Thread Po Lu
It has been brought to my attention that a defective memchr is distributed with certain Android E-book readers with Android 2.3.5 that is liable to integer overflows with large values of N: memchr ("", 0, SIZE_MAX); returns NULL, since: Dump of assembler code for function memchr: 0xafd20644