Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c1c14b

Browse files
committed
curl: initialize and cleanup global curl state
The documentation of curl states that any program making use of libcurl is responsible for calling `curl_global_init` previous to using any curl functions, and `curl_global_cleanup` when the program is about to terminate. We currently call neither. While this seems to work just fine, Valgrind may complain about unfree'd memory. Fix the issue by correctly setting up and tearing down libcurl.
1 parent c9d59c6 commit 8c1c14b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/global.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "sysdir.h"
1212
#include "filter.h"
1313
#include "merge_driver.h"
14+
#include "streams/curl.h"
1415
#include "streams/openssl.h"
1516
#include "thread-utils.h"
1617
#include "git2/global.h"
@@ -23,7 +24,7 @@
2324

2425
git_mutex git__mwindow_mutex;
2526

26-
#define MAX_SHUTDOWN_CB 9
27+
#define MAX_SHUTDOWN_CB 10
2728

2829
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
2930
static git_atomic git__n_shutdown_callbacks;
@@ -63,7 +64,8 @@ static int init_common(void)
6364
(ret = git_filter_global_init()) == 0 &&
6465
(ret = git_merge_driver_global_init()) == 0 &&
6566
(ret = git_transport_ssh_global_init()) == 0 &&
66-
(ret = git_openssl_stream_global_init()) == 0)
67+
(ret = git_openssl_stream_global_init()) == 0 &&
68+
(ret = git_curl_stream_global_init()) == 0)
6769
ret = git_mwindow_global_init();
6870

6971
GIT_MEMORY_BARRIER;

src/streams/curl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "stream.h"
1515
#include "git2/transport.h"
1616
#include "buffer.h"
17+
#include "global.h"
1718
#include "vector.h"
1819
#include "proxy.h"
1920

@@ -38,6 +39,17 @@ typedef struct {
3839
git_cred *proxy_cred;
3940
} curl_stream;
4041

42+
int git_curl_stream_global_init(void)
43+
{
44+
if (curl_global_init(CURL_GLOBAL_ALL) != 0) {
45+
giterr_set(GITERR_NET, "could not initialize curl");
46+
return -1;
47+
}
48+
49+
git__on_shutdown(curl_global_cleanup);
50+
return 0;
51+
}
52+
4153
static int seterr_curl(curl_stream *s)
4254
{
4355
giterr_set(GITERR_NET, "curl error: %s\n", s->curl_error);

src/streams/curl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "git2/sys/stream.h"
1313

14+
extern int git_curl_stream_global_init(void);
1415
extern int git_curl_stream_new(git_stream **out, const char *host, const char *port);
1516

1617
#endif

0 commit comments

Comments
 (0)