Add new function wget_free()

* examples/check_url_types.c: Use wget_xfree() instead of free()
* examples/http_get2.c: Likewise
* examples/print_css_urls2.c: Likewise
* examples/print_html_urls.c: Likewise
* include/wget/wget.h: Add wget_free() and define wget_xfree()
* libwget/xalloc.c: Implement wget_free()

* include/wget/wget.h: Add declaration for wget_free()
* libwget/xalloc.c: Implement wget_free()
This commit is contained in:
Tim Rühsen
2017-05-03 12:31:01 +02:00
parent e7b932b530
commit 7c1bebf9a0
6 changed files with 31 additions and 25 deletions

View File

@ -23,7 +23,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <signal.h>
@ -74,13 +73,13 @@ static int _normalize_uri(wget_iri_t *base, wget_string_t *url, const char *enco
wget_iri_unescape_inline(urlpart);
rc = wget_memiconv(encoding, urlpart, strlen(urlpart), "utf-8", &urlpart_encoded, &urlpart_encoded_length);
free(urlpart);
wget_xfree(urlpart);
if (rc)
return -2;
rc = !wget_iri_relative_to_abs(base, urlpart_encoded, urlpart_encoded_length, buf);
free(urlpart_encoded);
wget_xfree(urlpart_encoded);
if (rc)
return -3;
@ -219,7 +218,7 @@ static void html_parse(const char *html, size_t html_len, const char *encoding,
wget_iri_free(&allocated_base);
wget_html_free_urls_inline(&parsed);
wget_iri_free(&base);
free(utf8);
wget_xfree(utf8);
wget_info_printf(" same host: http=%d https=%d\n", stats.http_links_same_host, stats.https_links_same_host);
wget_info_printf(" total: http=%d https=%d\n", stats.http_links, stats.https_links);
@ -258,7 +257,7 @@ int main(int argc G_GNUC_WGET_UNUSED, const char *const *argv G_GNUC_WGET_UNUSED
while (fscanf(stdin, "%d,%255s", &stats.id, stats.host) == 2) {
free(url);
wget_xfree(url);
if (!wget_strncasecmp_ascii(stats.host, "http://", 7))
url = wget_aprintf("https://%s", stats.host + 7);
else if (wget_strncasecmp_ascii(stats.host, "https://", 8))
@ -308,7 +307,7 @@ int main(int argc G_GNUC_WGET_UNUSED, const char *const *argv G_GNUC_WGET_UNUSED
wget_info_printf(" Failed to normalize '%s', '%s'\n", url, resp->location);
break;
}
free(url);
wget_xfree(url);
url = newurl;
if (wget_strncasecmp(url, "https://", 8))
@ -348,7 +347,7 @@ int main(int argc G_GNUC_WGET_UNUSED, const char *const *argv G_GNUC_WGET_UNUSED
write_stats();
}
free(url);
wget_xfree(url);
// free resources - needed for valgrind testing
wget_global_deinit();

View File

@ -27,7 +27,6 @@
*
*/
#include <stdlib.h>
#include <wget.h>
#define COOKIE_SUPPORT
@ -104,7 +103,7 @@ int main(int argc G_GNUC_WGET_UNUSED, const char *const *argv G_GNUC_WGET_UNUSED
// enrich the HTTP request with the uri-related cookies we have
if ((cookie_string = wget_cookie_create_request_header(cookies, uri))) {
wget_http_add_header(req, "Cookie", cookie_string);
free((void *)cookie_string);
wget_xfree(cookie_string);
}
#endif

View File

@ -42,9 +42,6 @@
#define error_printf wget_error_printf
#define error_printf_exit wget_error_printf_exit
// I try to never leave freed pointers hanging around
#define xfree(a) do { if (a) { free((void *)(a)); a=NULL; } } while (0)
struct css_context {
wget_iri_t
*base;
@ -113,7 +110,7 @@ static void css_parse_localfile(const char *fname, wget_iri_t *base, const char
wget_css_parse_file(fname, css_parse_uri, css_parse_encoding, &context);
if (context.encoding_allocated)
xfree(context.encoding);
wget_xfree(context.encoding);
wget_buffer_deinit(&context.uri_buf);
}

View File

@ -33,11 +33,11 @@
static void html_parse_localfile(const char *fname)
{
char *data;
char *data, *data_allocated;
const char *encoding = NULL;
size_t len;
if ((data = wget_read_file(fname, &len))) {
if ((data_allocated = data = wget_read_file(fname, &len))) {
if ((unsigned char)data[0] == 0xFE && (unsigned char)data[1] == 0xFF) {
// Big-endian UTF-16
encoding = "UTF-16BE";
@ -72,8 +72,8 @@ static void html_parse_localfile(const char *fname)
if (wget_memiconv(encoding, data, len, "UTF-8", &utf8, &n) == 0) {
printf("Convert non-ASCII encoding '%s' to UTF-8\n", encoding);
wget_xfree(data);
data = utf8;
wget_xfree(data_allocated);
data_allocated = data = utf8;
} else {
printf("Failed to convert non-ASCII encoding '%s' to UTF-8, skip parsing\n", encoding);
return;
@ -94,7 +94,7 @@ static void html_parse_localfile(const char *fname)
printf(" %s.%s '%.*s'\n", html_url->dir, html_url->attr, (int) url->len, url->p);
}
wget_xfree(data);
wget_xfree(data_allocated);
wget_html_free_urls_inline(&res);
}
}

View File

@ -389,7 +389,7 @@ WGETAPI int
*/
// I try to never leave freed pointers hanging around
#define wget_xfree(a) do { if (a) { free((void *)(a)); a=NULL; } } while (0)
#define wget_xfree(a) do { if (a) { wget_free((void *)(a)); a=NULL; } } while (0)
typedef void (*wget_oom_callback_t)(void);
@ -399,6 +399,8 @@ WGETAPI void *
wget_calloc(size_t nmemb, size_t size) G_GNUC_WGET_MALLOC G_GNUC_WGET_ALLOC_SIZE2(1,2);
WGETAPI void *
wget_realloc(void *ptr, size_t size) G_GNUC_WGET_ALLOC_SIZE(2);
WGETAPI void
wget_free(void *ptr);
WGETAPI void
wget_set_oomfunc(wget_oom_callback_t);

View File

@ -127,12 +127,21 @@ void *wget_realloc(void *ptr, size_t size)
return p;
}
/*void wget_free(const void **p)
/**
* \param[in] ptr Pointer to memory-pointer to be freed
*
* This function is like free().
*
* Is is basically needed on systems where the library malloc heap is different
* from the caller's malloc heap, which happens on Windows when the library
* is a separate DLL.
*
* To prevent typical use-after-free issues, use the macro wget_xfree().
*/
void wget_free(void *ptr)
{
if (p && *p) {
free(*p);
*p = NULL;
}
}*/
if (ptr)
free(ptr);
}
/**@}*/