Source
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/* this file is for the minimal possible set of string related functions that are used by libzbxcommon.a */
/* or libraries/files it depends on */
/******************************************************************************
* *
* Purpose: Secure version of vsnprintf function. *
* Add zero character at the end of string. *
* *
* Parameters: str - [IN/OUT] destination buffer pointer *
* count - [IN] size of destination buffer *
* fmt - [IN] format *
* *
* Return value: the number of characters in the output buffer *
* (not including the trailing '\0') *
* *
******************************************************************************/
size_t zbx_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
int written_len = 0;
if (0 < count)
{
if (0 > (written_len = vsnprintf(str, count, fmt, args)))
written_len = (int)count - 1; /* count an output error as a full buffer */
else
written_len = MIN(written_len, (int)count - 1); /* result could be truncated */
}
str[written_len] = '\0'; /* always write '\0', even if buffer size is 0 or vsnprintf() error */
return (size_t)written_len;
}
int zbx_vsnprintf_check_len(const char *fmt, va_list args)
{
int rv;
if (0 > (rv = vsnprintf(NULL, 0, fmt, args)))
{
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
return rv;
}
/******************************************************************************
* *