Blame gl/vsnprintf.c

Packit Service 4684c1
/* Formatted output to strings.
Packit Service 4684c1
   Copyright (C) 2004, 2006-2020 Free Software Foundation, Inc.
Packit Service 4684c1
   Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software; you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU Lesser General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 2.1, or (at your option)
Packit Service 4684c1
   any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU Lesser General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU Lesser General Public License along
Packit Service 4684c1
   with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
#ifdef HAVE_CONFIG_H
Packit Service 4684c1
# include <config.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
/* Specification.  */
Packit Service 4684c1
#include <stdio.h>
Packit Service 4684c1
Packit Service 4684c1
#include <errno.h>
Packit Service 4684c1
#include <limits.h>
Packit Service 4684c1
#include <stdarg.h>
Packit Service 4684c1
#include <stdlib.h>
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
Packit Service 4684c1
#include "vasnprintf.h"
Packit Service 4684c1
Packit Service 4684c1
/* Print formatted output to string STR.  Similar to vsprintf, but
Packit Service 4684c1
   additional length SIZE limit how much is written into STR.  Returns
Packit Service 4684c1
   string length of formatted string (which may be larger than SIZE).
Packit Service 4684c1
   STR may be NULL, in which case nothing will be written.  On error,
Packit Service 4684c1
   return a negative value.  */
Packit Service 4684c1
int
Packit Service 4684c1
vsnprintf (char *str, size_t size, const char *format, va_list args)
Packit Service 4684c1
{
Packit Service 4684c1
  char *output;
Packit Service 4684c1
  size_t len;
Packit Service 4684c1
  size_t lenbuf = size;
Packit Service 4684c1
Packit Service 4684c1
  output = vasnprintf (str, &lenbuf, format, args);
Packit Service 4684c1
  len = lenbuf;
Packit Service 4684c1
Packit Service 4684c1
  if (!output)
Packit Service 4684c1
    return -1;
Packit Service 4684c1
Packit Service 4684c1
  if (output != str)
Packit Service 4684c1
    {
Packit Service 4684c1
      if (size)
Packit Service 4684c1
        {
Packit Service 4684c1
          size_t pruned_len = (len < size ? len : size - 1);
Packit Service 4684c1
          memcpy (str, output, pruned_len);
Packit Service 4684c1
          str[pruned_len] = '\0';
Packit Service 4684c1
        }
Packit Service 4684c1
Packit Service 4684c1
      free (output);
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
  if (len > INT_MAX)
Packit Service 4684c1
    {
Packit Service 4684c1
      errno = EOVERFLOW;
Packit Service 4684c1
      return -1;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
  return len;
Packit Service 4684c1
}