Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a crash when using musl #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matttbe
Copy link

@matttbe matttbe commented May 31, 2022

Without this PR, a warning is emitted when compiling libyaml with musl libc:

api.c: In function 'yaml_strdup':
api.c:66:27: warning: implicit declaration of function 'strdup'; did you mean 'strcmp'? [-Wimplicit-function-declaration]
     return (yaml_char_t *)strdup((char *)str);
                           ^~~~~~
                           strcmp

If the warning is ignored (default behaviour), a program using libyaml starts but crashes because strdup() is returning garbage:

(gdb) b parser.c:1365
Breakpoint 1 at 0x43ecf4: file parser.c, line 1365.
(gdb) run
(...)
Breakpoint 1, yaml_parser_append_tag_directive (
    parser=parser@entry=0x7ffffff5f0, value=...,
    allow_duplicates=allow_duplicates@entry=1, mark=...) at parser.c:1365
1365    in parser.c
(gdb) p copy
$1 = {
  handle = 0xffffffffb8000fe0 <error: Cannot access memory at address 0xffffffffb8000fe0>,
  prefix = 0xffffffffb7f76fe0 <error: Cannot access memory at address 0xffffffffb7f76fe0>}

The solution is to follow strdup()'s manpage and define _GNU_SOURCE before including string.h.

I guess when using other libc, it fallbacks to another version or maybe _GNU_SOURCE is set by default but it certainly uses a valid strdup.

Fixes: 625fcfe ("Refactor internal and external API.")

Without that, a warning is emitted when compiling libyaml with musl
libc:

  api.c: In function 'yaml_strdup':
  api.c:66:27: warning: implicit declaration of function 'strdup'; did you mean 'strcmp'? [-Wimplicit-function-declaration]
       return (yaml_char_t *)strdup((char *)str);
                             ^~~~~~
                             strcmp

If the warning is ignored, a program using libyaml starts but crashes
because strdup() is returning garbage:

  (gdb) b parser.c:1365
  Breakpoint 11 at 0x43ecf4: file parser.c, line 1365.
  (gdb) run
  (...)
  Breakpoint 1, yaml_parser_append_tag_directive (
      parser=parser@entry=0x7ffffff5f0, value=...,
      allow_duplicates=allow_duplicates@entry=1, mark=...) at parser.c:1365
  1365    in parser.c
  (gdb) p copy
  $1 = {
    handle = 0xffffffffb8000fe0 <error: Cannot access memory at address 0xffffffffb8000fe0>,
    prefix = 0xffffffffb7f76fe0 <error: Cannot access memory at address 0xffffffffb7f76fe0>}

The solution is to follow strdup()'s manpage and define _GNU_SOURCE
before including string.h.

I guess when using other libc, it fallbacks to another version or maybe
_GNU_SOURCE is set by default.

Fixes: 625fcfe ("Refactor internal and external API.")
Signed-off-by: Matthieu Baerts <[email protected]>
Change-Id: Ida011378a9d4da24735c4d4fdfdfb9d5f80ec970
@matttbe
Copy link
Author

matttbe commented May 31, 2022

It looks like the error reported by the CI is not due to this modification:

Step 1/8 : FROM fedora:25
25: Pulling from library/fedora
toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant