babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

Segfault when tried to access request->map_post_body #184

Closed NakedSolidSnake closed 3 years ago

NakedSolidSnake commented 3 years ago

Describe the issue I'm getting a segfault when I'm trying to get the post data. I'm using simple_example as a guide.

To Reproduce To reproduce the bug get the project in https://github.com/NakedSolidSnake/C_Webserver_Registry I'm using postman to send message in POST endpoint: /insert data: fname=Name&faddress=Address&fage=42

Expected behavior I want to get variable values

System (please complete the following information): AME="Linux Mint" VERSION="19.1 (Tessa)" ID=linuxmint ID_LIKE=ubuntu PRETTY_NAME="Linux Mint 19.1" VERSION_ID="19.1" HOME_URL="https://www.linuxmint.com/" SUPPORT_URL="https://forums.ubuntu.com/" BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/" PRIVACY_POLICY_URL="https://www.linuxmint.com/" VERSION_CODENAME=tessa UBUNTU_CODENAME=bionic

Additional context Thread 3 "MHD-connection" hit Breakpoint 1, callback_insert (request=0x7fffe4000b70, response=0x7fffe4001900, user_data=0x0) at /media/cssouza/SOLIDCRIS/repositories/C_Language/C_Webserver_Registry/src/main.c:73 73 char url_params = print_map(request->map_url); (gdb) p request $1 = {http_protocol = 0x7fffe4000e90 "HTTP/1.1", http_verb = 0x7fffe4000eb0 "POST", http_url = 0x7fffe4000e10 "/insert", url_path = 0x0, proxy = 0x1 <error: Cannot access memory at address 0x1>, network_type = 0, check_server_certificate = 0, check_server_certificate_flag = -469758256, check_proxy_certificate = 32767, check_proxy_certificate_flag = 0, follow_redirect = 0, ca_path = 0x0, timeout = 140737018596336, client_address = 0x7fffe4000c20, auth_basic_user = 0x7fffe4000c50 "", auth_basic_password = 0x7fffe4000c80 "", map_url = 0x7fffe4000fb0, map_header = 0x27, map_cookie = 0x35, map_post_body = 0x0, binary_body = 0x7fffe4000cd0, binary_body_length = 140737018596528, callback_position = 3959459072, client_cert = 0x0, client_cert_file = 0x35 <error: Cannot access memory at address 0x35>, client_key_file = 0xd <error: Cannot access memory at address 0xd>, client_key_password = 0x7fffe40016c0 "\360\016"} (gdb) c Continuing.

Thread 3 "MHD-connection" received signal SIGSEGV, Segmentation fault. 0x0000555555554fa5 in print_map (map=0x7fffe4000fb0) at /media/cssouza/SOLIDCRIS/repositories/C_Language/C_Webserver_Registry/src/main.c:20 20 for (i=0; keys[i] != NULL; i++) { (gdb) bt

0 0x0000555555554fa5 in print_map (map=0x7fffe4000fb0) at /media/cssouza/SOLIDCRIS/repositories/C_Language/C_Webserver_Registry/src/main.c:20

1 0x000055555555501a in callback_insert (request=0x7fffe4000b70, response=0x7fffe4001900, user_data=0x0)

at /media/cssouza/SOLIDCRIS/repositories/C_Language/C_Webserver_Registry/src/main.c:73

2 0x00007ffff7bc6e21 in ?? () from /usr/lib/libulfius.so.2.2

3 0x00007ffff73b45f5 in ?? () from /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.12

4 0x00007ffff73b61a8 in ?? () from /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.12

5 0x00007ffff73b8016 in ?? () from /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.12

6 0x00007ffff73bb558 in ?? () from /usr/lib/x86_64-linux-gnu/libmicrohttpd.so.12

7 0x00007ffff6f946db in start_thread (arg=0x7ffff0e38700) at pthread_create.c:463

8 0x00007ffff76ee88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

babelouest commented 3 years ago

Hello @NakedSolidSnake ,

I can't reproduce your bug by using your code and executing the curl command:

$ curl -i --data "fname=Name&faddress=Address&fage=42" http://localhost:8095/insert
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 0
Access-Control-Allow-Origin: *
Date: Sun, 07 Feb 2021 14:26:28 GMT

Besides the fact that your issue is in a useless part of the code in your case (you don't use url_params anywhere after setting it), there is no problem with your code that could lead to a segfault. I believe your problem is elsewhere.

How did you install ulfius? did you compile from the source? Or install a package downloaded from github? Or something else? If you installed a package that may be the problem: there is no package for Linux Mint. I suggest to build ulfius from the source and retry.

NakedSolidSnake commented 3 years ago

How did you install ulfius? did you compile from the source? Or install a package downloaded from github? Or something else? R: I downloaded from github, compiled and installed. If you installed a package that may be the problem: there is no package for Linux Mint. I suggest to build ulfius from the source and retry. C: Yeah I tried both, but no one works fine, maybe can be the operation system flavor, I'll try in another. But there is some strange about addresses showed in gdb when I printed variable request. Is there somewhere that I need to initialize the request buffers to works fine? I saw in the samples there is no need. But who knows?

NakedSolidSnake commented 3 years ago

Yeah, You're right. Doesn't work in that Linux flavor. I ran on my raspberry pi works fine. Thanks for your attention.

babelouest commented 3 years ago

Thanks for the confirmation, I'll try to test it with a Linux Mint to try to reproduce the problem.