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

sscanf produces segfault #1059

Open
mateuszkobak opened this issue Apr 19, 2024 · 3 comments · May be fixed by phoenix-rtos/libphoenix#357
Open

sscanf produces segfault #1059

mateuszkobak opened this issue Apr 19, 2024 · 3 comments · May be fixed by phoenix-rtos/libphoenix#357

Comments

@mateuszkobak
Copy link
Contributor

The following code produces a segmentation fault:

char hostname[60] = "[2001:aaa:aaaa:a::a]:8000";
char endbracket;
int len;

sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n", &endbracket, &len);
printf("%s\n%c\n%d\n", hostname, endbracket, len);

This is a reproduction of a situation in function Curl_parse_port in curl-7.64.1/lib/urlapi.c.

@Le0nyx
Copy link

Le0nyx commented Apr 19, 2024

char hostname[60] = "[2001:aaa:aaaa:a::a]:8000";
int len;

sscanf(hostname, "[%45[0123456789abcdefABCDEF:.]]%n", hostname, &len);
printf("%s\n%d\n", hostname, len);

Howcome you need the "endbracket" variable?
The code above might help

@anglov anglov transferred this issue from phoenix-rtos/libphoenix Apr 19, 2024
@anglov
Copy link
Member

anglov commented Apr 19, 2024

  1. Please create issues only in phoenix-rtos-project
  2. At the first glance reproduction code is valid (working as expected on linux glibc environment under --std=c99 and -pedantic), so probably there is some issue

Side note: curl upstream majorly modified Curl_parse_port function

@nalajcie
Copy link
Member

char hostname[60] = "[2001:aaa:aaaa:a::a]:8000";
int len;

sscanf(hostname, "[%45[0123456789abcdefABCDEF:.]]%n", hostname, &len);
printf("%s\n%d\n", hostname, len);

Howcome you need the "endbracket" variable? The code above might help

Please note that this is a reproduction code from curl, so the intent is not to change the code but check why it fails for our implementation of sscanf.

Regarding why the ] is not used directly - see the original code (https://github.com/curl/curl/blob/curl-7_64_1/lib/urlapi.c#L501) - it's used to detect the zoneidx encoding (eg. [fe80::20c:29ff:fe9c:409b%eth0]:1234 (if endbracket == '%')

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

Successfully merging a pull request may close this issue.

4 participants