Skip to content

Commit 6cf6aca

Browse files
committed
[acquire/acquire_net_common.h] Fix C++ support ; [acquire/acquire_url_utils.h] Fix memory leak
1 parent c614a06 commit 6cf6aca

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

acquire/acquire_net_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define LIBACQUIRE_ACQUIRE_NET_COMMON_H
88

99
#ifdef __cplusplus
10-
}
10+
extern "C" {
1111
#endif /* __cplusplus */
1212

1313
#include "acquire_download.h"

acquire/acquire_url_utils.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ extern LIBACQUIRE_LIB_EXPORT bool is_url(const char *);
2828
#ifdef LIBACQUIRE_IMPLEMENTATION
2929

3030
const char *get_path_from_url(const char *url) {
31+
static char buf[NAME_MAX + 1];
32+
const char *last_slash;
3133
size_t i;
32-
char *end_possible_query;
33-
if (url[0] == '\0')
34+
35+
if (!url || url[0] == '\0')
3436
return NULL;
35-
end_possible_query = strdup(url);
36-
end_possible_query = strrchr(end_possible_query, '/') + 1;
37-
if (end_possible_query == NULL)
38-
return end_possible_query;
39-
for (i = 0; i < strlen(end_possible_query); i++)
40-
if (end_possible_query[i] == '?' || end_possible_query[i] == '#') {
41-
end_possible_query[i] = '\0';
37+
38+
last_slash = strrchr(url, '/');
39+
if (last_slash)
40+
last_slash++;
41+
else
42+
last_slash = url;
43+
44+
for (i = 0; i < NAME_MAX && last_slash[i] != '\0'; i++) {
45+
if (last_slash[i] == '?' || last_slash[i] == '#')
4246
break;
43-
}
44-
return end_possible_query;
47+
buf[i] = last_slash[i];
48+
}
49+
buf[i] = '\0';
50+
51+
return buf;
4552
}
4653

4754
bool is_url(const char *maybe_url) {

0 commit comments

Comments
 (0)