<commit_before>/*
 * Copyright (c) 2003-2013  <NAME>
 * Copyright (c) 2010-2020  <NAME>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of the copyright holders nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>

#include "netcalc.h"
#include "output.h"

char *ident = PACKAGE_NAME;
extern char *optarg;
extern int optind, opterr, optopt;

int
out_cmdline(struct if_info *ifarg_cur, int v4args, struct misc_args m_argv4,
	    int v6args, struct misc_args m_argv6)
{
	int ret = 0;

	if ((v4args & V4CHECK) == V4CHECK || (v6args & V6CHECK) == V6CHECK) {
		if ((v4args & V4CHECK) == V4CHECK && strlen(ifarg_cur->p_v4addr) > 0) {
			if (get_addrv4(ifarg_cur) == 0)
				printf("%s\n", ifarg_cur->p_v4addr);
		} else if ((v6args & V6CHECK) == V6CHECK && strlen(ifarg_cur->p_v6addr) > 0) {
			if (get_addrv6(ifarg_cur) == 0)
				printf("%s\n", ifarg_cur->p_v6addr);
		}

		return 0;
	}

	if (ifarg_cur->type == IFT_V4)
		ret = get_addrv4(ifarg_cur);

	if (ifarg_cur->type == IFT_V6)
		ret = get_addrv6(ifarg_cur);

	if (ifarg_cur->type == IFT_INTV4 || ifarg_cur->type == IFT_INTV6) {
		if (ifarg_cur->errorstr[0] != 0) {
			warnx("Invalid address: %s", ifarg_cur->errorstr);
			return 0;
		}

		ret = get_addrv4(ifarg_cur);
	}

	if (ifarg_cur->type == IFT_UNKWN) {
		warnx("Unknown %s: %s", ifarg_cur->cmdstr, ifarg_cur->errorstr);
		return 0;
	}

	if (ret == -1) {
		warnx("Invalid address");
		return 0;
	}
	if (ret == -2) {
		warnx("Invalid netmask");
		return 0;
	}

	if (ifarg_cur->type == IFT_V4 || ifarg_cur->type == IFT_INTV4) {
		if (!v4args)
			v4args = V4_INFO;

		if ((v4args & V4_INFO) == V4_INFO)
			print_cf_info_v4(ifarg_cur);
		if ((v4args & V4SPLIT) == V4SPLIT)
			show_split_networks_v4(ifarg_cur, m_argv4.splitmask);
		if ((v4args & V4RANGE) == V4RANGE)
			show_network_ranges_v4(ifarg_cur, m_argv4.range_min, m_argv4.range_max);

		printf("\n");
	}

	if (ifarg_cur->type == IFT_V6 || ifarg_cur->type == IFT_INTV6) {
		if (!v6args)
			v6args = V6_INFO;

		if ((v6args & V6_INFO) == V6_INFO)
			print_v6(ifarg_cur);
		if ((v6args & V4INV6) == V4INV6)
			print_v4inv6(ifarg_cur);
		if ((v6args & V6REV) == V6REV)
			print_rev_v6(ifarg_cur);
		if ((v6args & V6SPLIT) == V6SPLIT)
			show_split_networks_v6(ifarg_cur, m_argv6.v6splitmask);

		printf("\n");
	}

	return 0;
}

int cleanline(char *sbuf, char *dbuf)
{
	int x, y;

	for (x = 0; x < (int)strlen(sbuf); x++) {
		if (sbuf[x] == '\n')
			sbuf[x] = ' ';
		if (sbuf[x] == '\t')
			sbuf[x] = ' ';
		if (sbuf[x] == '#')
			sbuf[x] = '\0';
	}
	x = strlen(sbuf) - 1;
	while (sbuf[x] == ' ' && x > -1) {
		sbuf[x] = '\0';
		x--;
	}
	if (!strlen(sbuf))
		return 0;

	x = 0;
	y = 0;
	while (x < (int)strlen(sbuf)) {
		if (sbuf[x] == ' ' && x) {
			dbuf[y] = ' ';
			y++;
		}

		while (sbuf[x] == ' ' && x < (int)strlen(sbuf))
			x++;

		while (sbuf[x] != ' ' && x < (int)strlen(sbuf)) {
			dbuf[y] = sbuf[x];
			y++;
			x++;
		}

		if (dbuf[y - 1] == ' ')
			return 0;
	}
	if (dbuf[strlen(dbuf) - 1] == ' ')
		dbuf[strlen(dbuf) - 1] = '\0';

	y = 1;
	for (x = 0; x < (int)strlen(dbuf); x++) {
		if (dbuf[x] == ' ')
			y++;
	}

	return y;
}

int get_stdin(char *args[])
{
	char buf[2], sbuf[ARGLEN], dbuf[ARGLEN], *arg1, *arg2;
	int x, y, z, argmax;

	memset(buf, 0, sizeof(buf));

	argmax = ARGLEN - 1;
	arg1 = (char *)calloc(1, ARGLEN);
	arg2 = (char *)calloc(1, ARGLEN);
	memset(sbuf, 0, sizeof(sbuf));
	memset(dbuf, 0, sizeof(dbuf));

	while (!sbuf[0]) {
		x = 0;
		y = 0;
		memset(sbuf, 0, sizeof(sbuf));
		do {
			x = read(0, buf, 1);
			if (x == 1)
				sbuf[y] = buf[0];
			y++;
		} while (x > 0 && buf[0] != '\n' && y < ((int)sizeof(sbuf) - 1));
		if (x < 0) {
			free(arg1);
			free(arg2);
			return -1;
		}
		if (!x)
			break;

		while (buf[0] != '\n' && x == 1)
			x = read(0, buf, 1);
		if (x < 0) {
			free(arg1);
			free(arg2);
			return -1;
		}
		if (!x)
			break;
	};
	if (!sbuf[0]) {
		free(arg1);
		free(arg2);
		return -2;
	}

	x = cleanline(sbuf, dbuf);
	if (x < 1) {
		free(arg1);
		free(arg2);
		return x;
	}

	y = 0;
	while (y < (int)strlen(dbuf) && y < argmax && dbuf[y] != ' ') {
		arg1[y] = dbuf[y];
		y++;
	}
	y++;
	z = 0;
	while (y < (int)strlen(dbuf) && z < argmax && dbuf[y] != ' ') {
		arg2[z] = dbuf[y];
		y++;
		z++;
	}

	strlcpy(args[0], arg1, ARGLEN);
	strlcpy(args[1], arg2, ARGLEN);

	free(arg1);
	free(arg2);

	return x;
}<commit_msg>Fix out-of-bounds read, found by Coverity Scan<commit_after>/*
 * Copyright (c) 2003-2013  <NAME>
 * Copyright (c) 2010-2020  <NAME>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of the copyright holders nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>

#include "netcalc.h"
#include "output.h"

char *ident = PACKAGE_NAME;
extern char *optarg;
extern int optind, opterr, optopt;

int
out_cmdline(struct if_info *ifarg_cur, int v4args, struct misc_args m_argv4,
	    int v6args, struct misc_args m_argv6)
{
	int ret = 0;

	if ((v4args & V4CHECK) == V4CHECK || (v6args & V6CHECK) == V6CHECK) {
		if ((v4args & V4CHECK) == V4CHECK && strlen(ifarg_cur->p_v4addr) > 0) {
			if (get_addrv4(ifarg_cur) == 0)
				printf("%s\n", ifarg_cur->p_v4addr);
		} else if ((v6args & V6CHECK) == V6CHECK && strlen(ifarg_cur->p_v6addr) > 0) {
			if (get_addrv6(ifarg_cur) == 0)
				printf("%s\n", ifarg_cur->p_v6addr);
		}

		return 0;
	}

	if (ifarg_cur->type == IFT_V4)
		ret = get_addrv4(ifarg_cur);

	if (ifarg_cur->type == IFT_V6)
		ret = get_addrv6(ifarg_cur);

	if (ifarg_cur->type == IFT_INTV4 || ifarg_cur->type == IFT_INTV6) {
		if (ifarg_cur->errorstr[0] != 0) {
			warnx("Invalid address: %s", ifarg_cur->errorstr);
			return 0;
		}

		ret = get_addrv4(ifarg_cur);
	}

	if (ifarg_cur->type == IFT_UNKWN) {
		warnx("Unknown %s: %s", ifarg_cur->cmdstr, ifarg_cur->errorstr);
		return 0;
	}

	if (ret == -1) {
		warnx("Invalid address");
		return 0;
	}
	if (ret == -2) {
		warnx("Invalid netmask");
		return 0;
	}

	if (ifarg_cur->type == IFT_V4 || ifarg_cur->type == IFT_INTV4) {
		if (!v4args)
			v4args = V4_INFO;

		if ((v4args & V4_INFO) == V4_INFO)
			print_cf_info_v4(ifarg_cur);
		if ((v4args & V4SPLIT) == V4SPLIT)
			show_split_networks_v4(ifarg_cur, m_argv4.splitmask);
		if ((v4args & V4RANGE) == V4RANGE)
			show_network_ranges_v4(ifarg_cur, m_argv4.range_min, m_argv4.range_max);

		printf("\n");
	}

	if (ifarg_cur->type == IFT_V6 || ifarg_cur->type == IFT_INTV6) {
		if (!v6args)
			v6args = V6_INFO;

		if ((v6args & V6_INFO) == V6_INFO)
			print_v6(ifarg_cur);
		if ((v6args & V4INV6) == V4INV6)
			print_v4inv6(ifarg_cur);
		if ((v6args & V6REV) == V6REV)
			print_rev_v6(ifarg_cur);
		if ((v6args & V6SPLIT) == V6SPLIT)
			show_split_networks_v6(ifarg_cur, m_argv6.v6splitmask);

		printf("\n");
	}

	return 0;
}

int cleanline(char *sbuf, char *dbuf)
{
	int x, y;

	for (x = 0; x < (int)strlen(sbuf); x++) {
		if (sbuf[x] == '\n')
			sbuf[x] = ' ';
		if (sbuf[x] == '\t')
			sbuf[x] = ' ';
		if (sbuf[x] == '#')
			sbuf[x] = '\0';
	}
	x = strlen(sbuf) - 1;
	while (sbuf[x] == ' ' && x > -1) {
		sbuf[x] = '\0';
		x--;
	}
	if (!strlen(sbuf))
		return 0;

	x = 0;
	y = 0;
	while (x < (int)strlen(sbuf)) {
		if (sbuf[x] == ' ' && x) {
			dbuf[y] = ' ';
			y++;
		}

		while (sbuf[x] == ' ' && x < (int)strlen(sbuf))
			x++;

		while (sbuf[x] != ' ' && x < (int)strlen(sbuf)) {
			dbuf[y] = sbuf[x];
			y++;
			x++;
		}

		if (dbuf[y - 1] == ' ')
			return 0;
	}
	if (dbuf[strlen(dbuf) - 1] == ' ')
		dbuf[strlen(dbuf) - 1] = '\0';

	y = 1;
	for (x = 0; x < (int)strlen(dbuf); x++) {
		if (dbuf[x] == ' ')
			y++;
	}

	return y;
}

int get_stdin(char *args[])
{
	char buf[2], sbuf[ARGLEN], dbuf[ARGLEN], *arg1, *arg2;
	int x, y, z, argmax;

	memset(buf, 0, sizeof(buf));

	argmax = ARGLEN - 1;
	arg1 = (char *)calloc(1, ARGLEN);
	arg2 = (char *)calloc(1, ARGLEN);
	memset(sbuf, 0, sizeof(sbuf));
	memset(dbuf, 0, sizeof(dbuf));

	while (!sbuf[0]) {
		x = 0;
		y = 0;
		memset(sbuf, 0, sizeof(sbuf));
		do {
			x = read(0, buf, 1);
			if (x == 1)
				sbuf[y] = buf[0];
			y++;
		} while (x > 0 && buf[0] != '\n' && y < ((int)sizeof(sbuf) - 1));
		if (x < 0) {
			free(arg1);
			free(arg2);
			return -1;
		}
		if (!x)
			break;

		while (buf[0] != '\n' && x == 1)
			x = read(0, buf, 1);
		if (x < 0) {
			free(arg1);
			free(arg2);
			return -1;
		}
		if (!x)
			break;
	};
	if (!sbuf[0]) {
		free(arg1);
		free(arg2);
		return -2;
	}

	x = cleanline(sbuf, dbuf);
	if (x < 1) {
		free(arg1);
		free(arg2);
		return x;
	}

	y = 0;
	while (y < (int)strlen(dbuf) && y < argmax && dbuf[y] != ' ') {
		arg1[y] = dbuf[y];
		y++;
	}
	y++;
	z = 0;
	while (y < (int)strlen(dbuf) && z < argmax && y < argmax && dbuf[y] != ' ') {
		arg2[z] = dbuf[y];
		y++;
		z++;
	}

	strlcpy(args[0], arg1, ARGLEN);
	strlcpy(args[1], arg2, ARGLEN);

	free(arg1);
	free(arg2);

	return x;
}
{'fraction_non_alphanumeric': 0.1679414978566025, 'fraction_numerical': 0.02891485248381945, 'mean_word_length': 3.3534577387486277, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 57, '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': '5344302', 'n_tokens_mistral': 5485, 'n_tokens_neox': 4942, 'n_words': 1300}