<commit_before>#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "callbacks.h"
#include "globals.h"
#include "error.h"

// Handler for libircclient errors
void irc_error(irc_session_t* irc_session, int fatal){
	int err = irc_errno(irc_session);
	const char* errstr = irc_strerror(err);
	error(fatal, "IRC error: %s (%d)\n", errstr, err);
}

// Print formatted error and if error is fatal, terminate
void error(int fatal, char* format, ...){
	char str[256];
	va_list args;
	va_start(args, format);
	// put the error msg in local array
	vsnprintf(str, 256, format, args);
	va_end(args);
	// print it out
	fprintf(stderr, "%s", str);
	
	if(fatal){
		fprintf(stderr,"Fatal error... Exiting.\n");
		exit(1);
	}
	else if(I && irc_is_connected(I)){
		// generate ERROR event to handle in scripts
		const char** strp = malloc(sizeof(char*));
		strp[0] = str;
		fprintf(stderr,"Generating ERROR event...\n");
		if(str[strlen(str)-1] == '\n')
			str[strlen(str)-1] = '\0';
		event_generic(I, "ERROR", "(core)", strp, 1);
		free(strp);
	}
	<commit_msg>fixed a potential infinite loop in the error handler<commit_after>#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "callbacks.h"
#include "globals.h"
#include "error.h"

// Handler for libircclient errors
void irc_error(irc_session_t* irc_session, int fatal){
	int err = irc_errno(irc_session);
	const char* errstr = irc_strerror(err);
	fprintf(stderr,"IRC error: %s (%d)\n", errstr, err);
	if(fatal){
		fprintf(stderr, "Fatal error... Exiting.\n");
	}
}

// Print formatted error and if error is fatal, terminate
void error(int fatal, char* format, ...){
	char str[256];
	va_list args;
	va_start(args, format);
	// put the error msg in local array
	vsnprintf(str, 256, format, args);
	va_end(args);
	// print it out
	fprintf(stderr, "%s", str);
	
	if(fatal){
		fprintf(stderr,"Fatal error... Exiting.\n");
		exit(1);
	}
	else if(I && irc_is_connected(I)){
		// generate ERROR event to handle in scripts
		const char** strp = malloc(sizeof(char*));
		strp[0] = str;
		fprintf(stderr,"Generating ERROR event...\n");
		if(str[strlen(str)-1] == '\n')
			str[strlen(str)-1] = '\0';
		event_generic(I, "ERROR", "(core)", strp, 1);
		free(strp);
	}
	
{'fraction_non_alphanumeric': 0.17596759675967596, 'fraction_numerical': 0.010801080108010801, 'mean_word_length': 4.330935251798561, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3758201', 'n_tokens_mistral': 890, 'n_tokens_neox': 834, 'n_words': 226}