Blame fuzz/libpsl_fuzzer.c

Packit 0af36a
/*
Packit 0af36a
 * Copyright(c) 2017-2018 Tim Ruehsen
Packit 0af36a
 *
Packit 0af36a
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit 0af36a
 * copy of this software and associated documentation files (the "Software"),
Packit 0af36a
 * to deal in the Software without restriction, including without limitation
Packit 0af36a
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit 0af36a
 * and/or sell copies of the Software, and to permit persons to whom the
Packit 0af36a
 * Software is furnished to do so, subject to the following conditions:
Packit 0af36a
 *
Packit 0af36a
 * The above copyright notice and this permission notice shall be included in
Packit 0af36a
 * all copies or substantial portions of the Software.
Packit 0af36a
 *
Packit 0af36a
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 0af36a
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 0af36a
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit 0af36a
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 0af36a
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit 0af36a
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 0af36a
 * DEALINGS IN THE SOFTWARE.
Packit 0af36a
 *
Packit 0af36a
 * This file is part of libpsl.
Packit 0af36a
 */
Packit 0af36a
Packit 0af36a
#include <config.h>
Packit 0af36a
Packit 0af36a
#include <assert.h> /* assert */
Packit 0af36a
Packit 0af36a
#ifdef HAVE_STDINT_H
Packit 0af36a
#include <stdint.h> /* uint8_t */
Packit 0af36a
#elif defined (_MSC_VER)
Packit 0af36a
typedef unsigned __int8 uint8_t;
Packit 0af36a
#endif
Packit 0af36a
Packit 0af36a
#include <stdlib.h> /* malloc, free */
Packit 0af36a
#include <string.h> /* memcpy */
Packit 0af36a
Packit 0af36a
#if defined(WITH_LIBICU)
Packit 0af36a
#include <unicode/uclean.h>
Packit 0af36a
#endif
Packit 0af36a
Packit 0af36a
#include "libpsl.h"
Packit 0af36a
#include "fuzzer.h"
Packit 0af36a
Packit 0af36a
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Packit 0af36a
{
Packit 0af36a
	static int first_run = 1;
Packit 0af36a
	psl_ctx_t *psl;
Packit 0af36a
	char *domain, *res;
Packit 0af36a
	int rc;
Packit 0af36a
Packit 0af36a
	if (size > 64 * 1024 - 1)
Packit 0af36a
		return 0;
Packit 0af36a
Packit 0af36a
	domain = (char *) malloc(size + 1);
Packit 0af36a
	assert(domain != NULL);
Packit 0af36a
Packit 0af36a
	/* 0 terminate */
Packit 0af36a
	memcpy(domain, data, size);
Packit 0af36a
	domain[size] = 0;
Packit 0af36a
Packit 0af36a
	psl = (psl_ctx_t *) psl_builtin();
Packit 0af36a
Packit 0af36a
	psl_is_public_suffix(psl, domain);
Packit 0af36a
	psl_is_public_suffix2(psl, domain, PSL_TYPE_PRIVATE);
Packit 0af36a
	psl_is_public_suffix2(psl, domain, PSL_TYPE_ICANN);
Packit 0af36a
	psl_is_public_suffix2(psl, domain, PSL_TYPE_NO_STAR_RULE);
Packit 0af36a
	psl_is_public_suffix2(psl, domain, PSL_TYPE_NO_STAR_RULE|PSL_TYPE_ANY);
Packit 0af36a
	psl_unregistrable_domain(psl, domain);
Packit 0af36a
	psl_registrable_domain(psl, domain);
Packit 0af36a
Packit 0af36a
	psl_is_cookie_domain_acceptable(psl, "", NULL);
Packit 0af36a
	psl_is_cookie_domain_acceptable(psl, "a.b.c.e.com", domain);
Packit 0af36a
Packit 0af36a
	if ((rc = psl_str_to_utf8lower(domain, "utf-8", NULL, &res)) == PSL_SUCCESS)
Packit 0af36a
		free(res);
Packit 0af36a
	if ((rc = psl_str_to_utf8lower(domain, "iso-8859-1", NULL, &res)) == PSL_SUCCESS)
Packit 0af36a
		free(res);
Packit 0af36a
	if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &res)) == PSL_SUCCESS)
Packit 0af36a
		free(res);
Packit 0af36a
Packit 0af36a
	psl_free(psl);
Packit 0af36a
Packit 0af36a
	if (first_run) {
Packit 0af36a
		psl_is_public_suffix(NULL, domain);
Packit 0af36a
		psl_check_version_number(1);
Packit 0af36a
		psl_get_version();
Packit 0af36a
		psl_dist_filename();
Packit 0af36a
		psl_builtin_outdated();
Packit 0af36a
		psl_builtin_filename();
Packit 0af36a
		psl_builtin_sha1sum();
Packit 0af36a
		psl_builtin_file_time();
Packit 0af36a
		first_run = 0;
Packit 0af36a
	}
Packit 0af36a
Packit 0af36a
	free(domain);
Packit 0af36a
Packit 0af36a
#if defined(WITH_LIBICU)
Packit 0af36a
	u_cleanup(); /* free all library internal memory to avoid memory leaks being reported */
Packit 0af36a
#endif
Packit 0af36a
Packit 0af36a
	return 0;
Packit 0af36a
}