<commit_before>/**
 * @file xml.c
 * @author <NAME> <<EMAIL>>
 * @brief XML parser implementation for libyang
 *
 * Copyright (c) 2015 CESNET, z.s.p.o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 */


#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"
#include "dict.h"
#include "tree.h"
#include "xml.h"

/*
 * Macro to test if character is #x20 | #x9 | #xA | #xD (whitespace)
 */
#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)

#define is_xmlnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
		(c >= 'A' && c <= 'Z') || c == ':' || \
		(c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
		(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
		c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
		(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
		(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
		(c >= 0x10000 && c <= 0xeffff))

#define is_xmlnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
		(c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ':' || \
		c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
		(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
		c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
		(c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
		(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
		(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
		(c >= 0x10000 && c <= 0xeffff))

#define ign_xmlws(p) while(is_xmlws(*p)) {p++;}

int lyxml_unlink_attr(struct lyxml_attr *attr)
{
	struct lyxml_attr *a;

	if (!attr) {
		LY_ERR(LY_EINVAL, NULL);
		return EXIT_FAILURE;
	}

	if (!attr->parent) {
		return EXIT_SUCCESS;
	}

	a = attr->parent->attr;
	if (!a) {
		LY_ERR(LY_EINVAL, "Broken structure (%s).", __func__);
		return EXIT_FAILURE;
	} else if (a == attr) {
		attr->parent->attr = attr->next;
	} else {
		while(a->next != attr) {
			a = a->next;
		}

		if (!a) {
			LY_ERR(LY_EINVAL, "Broken structure (%s).", __func__);
			return EXIT_FAILURE;
		}

		a->next = attr->next;
	}

	attr->parent = NULL;
	attr->next = NULL;

	return EXIT_SUCCESS;
}<commit_msg>xml parser: BUGFIX: NULL check before dereferencing pointer<commit_after>/**
 * @file xml.c
 * @author <NAME> <<EMAIL>>
 * @brief XML parser implementation for libyang
 *
 * Copyright (c) 2015 CESNET, z.s.p.o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 */


#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"
#include "dict.h"
#include "tree.h"
#include "xml.h"

/*
 * Macro to test if character is #x20 | #x9 | #xA | #xD (whitespace)
 */
#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)

#define is_xmlnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
		(c >= 'A' && c <= 'Z') || c == ':' || \
		(c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
		(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
		c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
		(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
		(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
		(c >= 0x10000 && c <= 0xeffff))

#define is_xmlnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
		(c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ':' || \
		c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
		(c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
		c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
		(c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
		(c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
		(c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
		(c >= 0x10000 && c <= 0xeffff))

#define ign_xmlws(p) while(is_xmlws(*p)) {p++;}

int lyxml_unlink_attr(struct lyxml_attr *attr)
{
	struct lyxml_attr *a;

	if (!attr) {
		LY_ERR(LY_EINVAL, NULL);
		return EXIT_FAILURE;
	}

	if (!attr->parent) {
		return EXIT_SUCCESS;
	}

	a = attr->parent->attr;
	if (!a) {
		LY_ERR(LY_EINVAL, "Broken structure (%s).", __func__);
		return EXIT_FAILURE;
	} else if (a == attr) {
		attr->parent->attr = attr->next;
	} else {
		while(a && a->next != attr) {
			a = a->next;
		}

		if (!a) {
			LY_ERR(LY_EINVAL, "Broken structure (%s).", __func__);
			return EXIT_FAILURE;
		}

		a->next = attr->next;
	}

	attr->parent = NULL;
	attr->next = NULL;

	return EXIT_SUCCESS;
}
{'fraction_non_alphanumeric': 0.20476431829700964, 'fraction_numerical': 0.05473897617840851, 'mean_word_length': 3.0437158469945356, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 21}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4835806', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2323, 'n_words': 690}