From: Bugs Reporter <bugs@compiler.ai>
To: newlib@sourceware.org
Cc: sbansal@iitd.ac.in, shubhani@sit.iitd.ac.in,
abhishek.rose@cse.iitd.ac.in, Jai.Arora.cs518@cse.iitd.ac.in
Subject: Bug in memccpy
Date: Wed, 28 Dec 2022 17:01:48 +0530 [thread overview]
Message-ID: <CAB_V0MAXh9QOBARo-G3YnxmBS5fk4_70cE33vMppgg4HvNe=8w@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1757 bytes --]
Hi,
I am writing to report a bug in the memccpy function of newlib.
The bug is in the C implementation of memccpy as located in the
newlib/libc/string/ directory of the newlib repository. The newlib version
was `4.2.0` and the source code was downloaded from the latest release
<https://sourceware.org/pub/newlib/newlib-4.2.0.20211231.tar.gz> on the
official website.
Please find a detailed report below.
memccpy():
As specified in the OpenBSD manpage[0], memccpy should convert the input
argument `c` to `unsigned char` before performing the check. Newlib's fast
implementation of `memccpy` (when the macros `PREFER_SIZE_OVER_SPEED` and
`__OPTIMIZE_SIZE__` are not defined) converts `c` to `signed char` (stored
in endchar) and computes a mask as follows:
```
for (i = 0; i < LITTLEBLOCKSIZE; i++)
mask = (mask << 8) + endchar;
```
This is used to detect `endchar` in one long word and is supposed to
represent a word whose each byte has the same value as `endchar`. But if
the input character lies in the extended ASCII set, then `endchar` is
negative, which leads to an incorrect mask computation.
An example input is:
const char src[] = {1, 2, 3, 4, 5, 192, 6, 7};
char dst[8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
memccpy(dst, src, 192, 8);
if (dst[7] != 'H') {
printf("BUG!\n");
}
The file that demonstrates the bug can be found here
<https://github.com/compilerai/bug-reports/blob/master/bug_files/newlib_memccpy_bug.c>
.
A patch that applies the necessary fix is available here
<https://github.com/compilerai/bug-reports/blob/master/patch/newlib_memccpy.patch>
.
0: https://man.openbsd.org/memccpy
Thanks,
Jai Arora, Abhishek Rose, Shubhani Gupta, Sorav Bansal
CompilerAI Research Group
IIT Delhi, India
next reply other threads:[~2022-12-28 11:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-28 11:31 Bugs Reporter [this message]
2023-01-03 19:54 ` Jeff Johnston
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAB_V0MAXh9QOBARo-G3YnxmBS5fk4_70cE33vMppgg4HvNe=8w@mail.gmail.com' \
--to=bugs@compiler.ai \
--cc=Jai.Arora.cs518@cse.iitd.ac.in \
--cc=abhishek.rose@cse.iitd.ac.in \
--cc=newlib@sourceware.org \
--cc=sbansal@iitd.ac.in \
--cc=shubhani@sit.iitd.ac.in \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).