<commit_before>/*
 *   cli_usock.c - Teamd daemon control library teamd Unix Domain socket client
 *   Copyright (C) 2013 <NAME> <<EMAIL>>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <teamdctl.h>
#include "teamdctl_private.h"
#include "../teamd/teamd_usock.h"

struct cli_usock_priv {
	int sock;
};

static int cli_usock_check_error_msg(struct teamdctl *tdc, char *msg)
{
	char *str;
	char *str2;

	if (!strncmp(TEAMD_USOCK_SUCC_PREFIX, msg,
		     strlen(TEAMD_USOCK_SUCC_PREFIX)))
		return 0;
	if (strncmp(TEAMD_USOCK_ERR_PREFIX, msg,
		    strlen(TEAMD_USOCK_ERR_PREFIX)))
		goto corrupted;

	str = strchr(msg, '\n');
	if (!str || str[1] == '\0')
		goto corrupted;
	str++;

	str2 = strchr(str, '\n');
	if (!str2 || str2[1] == '\0')
		goto corrupted;
	str2[0] = '\0';
	str2++;

	err(tdc, "Error message received: \"%s\"", str);

	str = strchr(str2, '\n');
	if (!str)
		goto corrupted;
	str[0] = '\0';

	err(tdc, "Error message content: \"%s\"", str2);

	return 0;
corrupted:
	err(tdc, "Corrupted message received.");
	return -EINVAL;
}

static char *cli_usock_get_reply_str(char *msg)
{
	char *str;

	str = strchr(msg, '\n');
	if (str)
		str++;
	return str;
}

static int cli_usock_send(int sock, char *msg)
{
	int err;

	err = send(sock, msg, strlen(msg), MSG_NOSIGNAL);
	if (err == -1)
		return -errno;
	return 0;
}

static int cli_usock_method_call(struct teamdctl *tdc, const char *method_name,
				 char **p_reply, void *priv,
				 const char *fmt, va_list ap)
{
	struct cli_usock_priv *cli_usock = priv;
	char *str;
	char *msg;
	char *recvmsg = recvmsg;
	char *reply;
	int err;

	dbg(tdc, "Calling method \"%s\"", method_name);
	err = asprintf(&msg, "%s\n", method_name);
	if (err == -1)
		return -ENOMEM;
	while (*fmt) {
		switch (*fmt++) {
		case 's': /* string */
			str = va_arg(ap, char *);
			err = asprintf(&msg, "%s%s\n", msg, str);
			if (err == -1) {
				err = -ENOMEM;
				goto free_msg;
			}
			break;
		default:
			err(tdc, "Unknown argument type requested.");
			err = -EINVAL;
			goto free_msg;
		}
	}

	err = asprintf(&msg, "%s\n", msg);
	if (err == -1) {
		err = -ENOMEM;
		goto free_msg;
	}
<commit_msg>libteamdctl: usock: fix memory lead composing message using asprintf<commit_after>/*
 *   cli_usock.c - Teamd daemon control library teamd Unix Domain socket client
 *   Copyright (C) 2013 <NAME> <<EMAIL>>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <teamdctl.h>
#include "teamdctl_private.h"
#include "../teamd/teamd_usock.h"

struct cli_usock_priv {
	int sock;
};

static int cli_usock_check_error_msg(struct teamdctl *tdc, char *msg)
{
	char *str;
	char *str2;

	if (!strncmp(TEAMD_USOCK_SUCC_PREFIX, msg,
		     strlen(TEAMD_USOCK_SUCC_PREFIX)))
		return 0;
	if (strncmp(TEAMD_USOCK_ERR_PREFIX, msg,
		    strlen(TEAMD_USOCK_ERR_PREFIX)))
		goto corrupted;

	str = strchr(msg, '\n');
	if (!str || str[1] == '\0')
		goto corrupted;
	str++;

	str2 = strchr(str, '\n');
	if (!str2 || str2[1] == '\0')
		goto corrupted;
	str2[0] = '\0';
	str2++;

	err(tdc, "Error message received: \"%s\"", str);

	str = strchr(str2, '\n');
	if (!str)
		goto corrupted;
	str[0] = '\0';

	err(tdc, "Error message content: \"%s\"", str2);

	return 0;
corrupted:
	err(tdc, "Corrupted message received.");
	return -EINVAL;
}

static char *cli_usock_get_reply_str(char *msg)
{
	char *str;

	str = strchr(msg, '\n');
	if (str)
		str++;
	return str;
}

static int cli_usock_send(int sock, char *msg)
{
	int err;

	err = send(sock, msg, strlen(msg), MSG_NOSIGNAL);
	if (err == -1)
		return -errno;
	return 0;
}

static int myasprintf(char **p_str, const char *fmt, ...)
{
	char *newstr;
	va_list ap;
	int ret;

	va_start(ap, fmt);
	ret = vasprintf(&newstr, fmt, ap);
	va_end(ap);
	if (ret == -1)
		return -ENOMEM;
	free(*p_str);
	*p_str = newstr;
	return 0;
}

static int cli_usock_method_call(struct teamdctl *tdc, const char *method_name,
				 char **p_reply, void *priv,
				 const char *fmt, va_list ap)
{
	struct cli_usock_priv *cli_usock = priv;
	char *str;
	char *msg = NULL;
	char *recvmsg = recvmsg;
	char *reply;
	int err;

	dbg(tdc, "Calling method \"%s\"", method_name);
	err= myasprintf(&msg, "%s\n", method_name);
	if (err)
		return err;
	while (*fmt) {
		switch (*fmt++) {
		case 's': /* string */
			str = va_arg(ap, char *);
			err = myasprintf(&msg, "%s%s\n", msg, str);
			if (err)
				goto free_msg;
			break;
		default:
			err(tdc, "Unknown argument type requested.");
			err = -EINVAL;
			goto free_msg;
		}
	}

	err = myasprintf(&msg, "%s\n", msg);
	if (err)
		goto free_msg;

{'fraction_non_alphanumeric': 0.14231014677728143, 'fraction_numerical': 0.012603701340140395, 'mean_word_length': 3.6129506990434144, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '4085067', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2341, 'n_words': 742}