mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Bug in swab
@ 2022-12-28 11:31 Bugs Reporter
  2022-12-28 12:06 ` NRK
  0 siblings, 1 reply; 4+ messages in thread
From: Bugs Reporter @ 2022-12-28 11:31 UTC (permalink / raw)
  To: musl; +Cc: sbansal, shubhani, abhishek.rose, Jai.Arora.cs518

[-- Attachment #1: Type: text/plain, Size: 1252 bytes --]

Hi,

I am writing to report a bug in the swab function of musl.
The bug is in the C implementation of swab as located in the src/string
directory of the musl repository. The musl version was `1.2.3` and the
source code was downloaded from the latest release
<https://musl.libc.org/releases/musl-1.2.3.tar.gz> on the official website.
Please find a detailed report below.

Linux[0] manpage for swab() specifies that the function copies n bytes from
the array pointed to by from to the array pointed to by to, exchanging
adjacent even and odd bytes. This should also work for coinciding memories
and musl's implementation does not take that into account.
An example input is:
    char src[] = {65, 64};
    void* dst = src;
    swab(src, dst, 2);
    if (src[0] != 64 || src[1] != 65) {
      printf("BUG!\n");
    }

The file that demonstrates the bug can be found here
<https://github.com/compilerai/bug-reports/blob/master/bug_files/musl_swab_bug.c>
.
A patch that applies the necessary fix is available here
<https://github.com/compilerai/bug-reports/blob/master/patch/musl_swab.patch>
.

0: https://man7.org/linux/man-pages/man3/swab.3.html

Thanks,

Jai Arora, Abhishek Rose, Shubhani Gupta, Sorav Bansal
CompilerAI Research Group
IIT Delhi, India

[-- Attachment #2: Type: text/html, Size: 1539 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Bug in swab
  2022-12-28 11:31 [musl] Bug in swab Bugs Reporter
@ 2022-12-28 12:06 ` NRK
  2022-12-28 16:20   ` Quentin Rameau
  2022-12-29  9:50   ` Alex Xu (Hello71)
  0 siblings, 2 replies; 4+ messages in thread
From: NRK @ 2022-12-28 12:06 UTC (permalink / raw)
  To: musl

On Wed, Dec 28, 2022 at 05:01:53PM +0530, Bugs Reporter wrote:
> This should also work for coinciding memories
> and musl's implementation does not take that into account.

The POSIX manpage declares the prototype of swab(3p) with the `restrict`
qualifier [0]. So calling this function with overlapping memory region is
not correct - the bug is in the calling code.

[0]: https://man7.org/linux/man-pages/man3/swab.3p.html

- NRK

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Bug in swab
  2022-12-28 12:06 ` NRK
@ 2022-12-28 16:20   ` Quentin Rameau
  2022-12-29  9:50   ` Alex Xu (Hello71)
  1 sibling, 0 replies; 4+ messages in thread
From: Quentin Rameau @ 2022-12-28 16:20 UTC (permalink / raw)
  To: musl

> > This should also work for coinciding memories
> > and musl's implementation does not take that into account.  
> 
> The POSIX manpage declares the prototype of swab(3p) with the `restrict`
> qualifier [0]. So calling this function with overlapping memory region is
> not correct - the bug is in the calling code.

On top of that, it explicitly specifies:
“If copying takes place between objects that overlap, the behavior is undefined”

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Bug in swab
  2022-12-28 12:06 ` NRK
  2022-12-28 16:20   ` Quentin Rameau
@ 2022-12-29  9:50   ` Alex Xu (Hello71)
  1 sibling, 0 replies; 4+ messages in thread
From: Alex Xu (Hello71) @ 2022-12-29  9:50 UTC (permalink / raw)
  To: musl

Excerpts from NRK's message of December 28, 2022 1:06 pm:
> On Wed, Dec 28, 2022 at 05:01:53PM +0530, Bugs Reporter wrote:
>> This should also work for coinciding memories
>> and musl's implementation does not take that into account.
> 
> The POSIX manpage declares the prototype of swab(3p) with the `restrict`
> qualifier [0]. So calling this function with overlapping memory region is
> not correct - the bug is in the calling code.
> 
> [0]: https://man7.org/linux/man-pages/man3/swab.3p.html
> 
> - NRK
> 

Actually, the originally linked 
https://man7.org/linux/man-pages/man3/swab.3.html also uses restrict. It 
was added in 2021: 
https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/man3/swab.3?id=cd2b76c7dc4a607d952f1ae77fdee2dd7dc9c3dc.

Cheers,
Alex.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-12-29  9:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-28 11:31 [musl] Bug in swab Bugs Reporter
2022-12-28 12:06 ` NRK
2022-12-28 16:20   ` Quentin Rameau
2022-12-29  9:50   ` Alex Xu (Hello71)

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).