Add new function psl_free_string()

When writing a wrapper around LibPSL in a different language it is
important that libpsl provide functions to free any memory that it
allocates. Without this, it is impossible to correctly free the memory
allocated by psl_str_to_utf8lower() function since in other languages
one may not have access to the same free() call from libc.
This commit is contained in:
Darshit Shah 2017-08-29 19:32:47 +02:00
parent d6c1051b37
commit 4d5982ed98
5 changed files with 25 additions and 6 deletions

View File

@ -79,6 +79,10 @@ typedef struct _psl_ctx_st psl_ctx_t;
void void
psl_free(psl_ctx_t *psl); psl_free(psl_ctx_t *psl);
/* frees memory allocated by libpsl routines */
void
psl_free_string(char *str);
/* loads PSL data from file */ /* loads PSL data from file */
psl_ctx_t * psl_ctx_t *
psl_load_file(const char *fname); psl_load_file(const char *fname);

View File

@ -1613,6 +1613,21 @@ int psl_is_cookie_domain_acceptable(const psl_ctx_t *psl, const char *hostname,
return 0; return 0;
} }
/**
* psl_free_string:
* @str: pointer to lowercase string returned by psl_str_to_utf8lower()
*
* This function free()'s the memory allocated by psl_str_to_utf8lower() when
* returning a lowercase string
*
* Since: 0.19
*/
void psl_free_string(char *str)
{
if (str)
free(str);
}
/** /**
* psl_str_to_utf8lower: * psl_str_to_utf8lower:
* @str: string to convert * @str: string to convert

View File

@ -133,16 +133,16 @@ static void test_psl(void)
char *lower = NULL; char *lower = NULL;
psl_str_to_utf8lower("www.example.com", NULL, "de", &lower); psl_str_to_utf8lower("www.example.com", NULL, "de", &lower);
free(lower); lower = NULL; psl_free_string(lower); lower = NULL;
psl_str_to_utf8lower("\374bel.de", NULL, "de", &lower); psl_str_to_utf8lower("\374bel.de", NULL, "de", &lower);
free(lower); lower = NULL; psl_free_string(lower); lower = NULL;
psl_str_to_utf8lower("\374bel.de", "iso-8859-1", NULL, &lower); psl_str_to_utf8lower("\374bel.de", "iso-8859-1", NULL, &lower);
free(lower); lower = NULL; psl_free_string(lower); lower = NULL;
psl_str_to_utf8lower(NULL, "utf-8", "en", &lower); psl_str_to_utf8lower(NULL, "utf-8", "en", &lower);
free(lower); lower = NULL; psl_free_string(lower); lower = NULL;
} }
psl_get_version(); psl_get_version();

View File

@ -81,7 +81,7 @@ static void testx(const psl_ctx_t *psl, const char *domain, const char *encoding
domain ? domain : "NULL", result ? result : "NULL", expected_result ? expected_result : "NULL"); domain ? domain : "NULL", result ? result : "NULL", expected_result ? expected_result : "NULL");
} }
free(lower); psl_free_string(lower);
} }
static void test(const psl_ctx_t *psl, const char *domain, const char *expected_result) static void test(const psl_ctx_t *psl, const char *domain, const char *expected_result)

View File

@ -182,7 +182,7 @@ int main(int argc, const char *const *argv)
} }
if (rc == PSL_SUCCESS) if (rc == PSL_SUCCESS)
free(lower); psl_free_string(lower);
} }
psl_free(psl); psl_free(psl);