I am trying to learn using libwget on Debian 12. I've installed the package "wget2-dev" which recursively installed "libwget0" package. I wrote a very basic and simple application with the help of wget2 examples.
#include <stdio.h>
#include <stdlib.h>
#include <wget.h>
int main(int argc, char *argv[])
{
wget_http_connection_t *conn = NULL;
wget_http_response_t *resp;
// set up libwget global configuration
wget_global_init(
WGET_COOKIES_ENABLED, 1,
WGET_COOKIE_SUFFIXES, "public_suffixes.txt",
WGET_COOKIE_FILE, "cookies.txt",
WGET_COOKIE_KEEPSESSIONCOOKIES, 1,
0);
// execute an HTTP GET request and return the response
resp = wget_http_get(
WGET_HTTP_URL, "http://example.com",
WGET_HTTP_MAX_REDIRECTIONS, 5,
WGET_HTTP_CONNECTION_PTR, &conn,
0);
if (resp)
{
printf("%s", resp->body->data);
// free the response
wget_http_free_response(&resp);
}
// close connection if still open
wget_http_close(&conn);
// free resources - needed for valgrind testing
wget_global_deinit();
return EXIT_SUCCESS;
}
My problem is that, while calling wget_global_init function, if I set "WGET_COOKIES_ENABLED" to "1", I get a Segmentation Fault.
What is the problem here? Is that because of the library or am I doing something wrong? Thanks.
EDIT: I tried the answer in another question related to Segmentation Fault. If I type
gdb ./MyApp
I get
(gdb) run
Starting program: MyApp
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
___pthread_mutex_lock (mutex=0x0) at ./nptl/pthread_mutex_lock.c:80
80 ./nptl/pthread_mutex_lock.c: No such file or directory.
What does that mean? How can I solve it?
EDIT 2: If I type
valgrind --leak-check=full ./MyApp
I get
==42170== Invalid read of size 4
==42170== at 0x49541C0: pthread_mutex_lock@@GLIBC_2.2.5 (pthread_mutex_lock.c:80)
==42170== by 0x487A7EC: wget_cookie_create_request_header (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x4895153: wget_http_get (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x1091EE: main (main.c:23)
==42170== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==42170==
==42170==
==42170== Process terminating with default action of signal 11 (SIGSEGV)
==42170== Access not within mapped region at address 0x10
==42170== at 0x49541C0: pthread_mutex_lock@@GLIBC_2.2.5 (pthread_mutex_lock.c:80)
==42170== by 0x487A7EC: wget_cookie_create_request_header (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x4895153: wget_http_get (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x1091EE: main (main.c:23)
Is that a bug, or am I not using the library correctly?
EDIT 3: After the comment by Olaf Dietsche, I added -lpthread
to Build Settings in Eclipse CDT
Invoking: GCC C Compiler
gcc -lpthread -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.c"
Invoking: GCC C Linker
gcc -lpthread -L/usr/lib -o "MyApp" ./src/main.o -lwget
I am still getting the same error when I run the program.
EDIT 4: I added -lpthread
to GCC Linker options too after another comment by Olaf Dietsche. And I am still getting Segmentation Fault.
It's a bug in the library (d51398947e150e930299f3c9657aaf6204489f4d) where wget_global_get_ptr()
returns a &_config.cookie_db
but caller wget_http_get()
expects a _config.cookie_db
. This fix shipped in v2.0.0 and the current version in Debian is 1.99.1-2.2.
@rockdaboot mentioned it's fixed in Debian testing so you could install that package from testing.
Once you get a fixed version of the library you probably want to specify WGET_COOKIE_FILE
which sets WGET_COOKIES_ENABLED
.
Please file report a bug to Debian against the package libwget0.
Bonus: It turns out that the wget (binary) doesn't depend on libwget0.