Blame snprintf/snprintf.c

Packit c04fcb
/*	$OpenBSD: snprintf.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
Packit c04fcb
/*-
Packit c04fcb
 * Copyright (c) 1990, 1993
Packit c04fcb
 *	The Regents of the University of California.  All rights reserved.
Packit c04fcb
 *
Packit c04fcb
 * This code is derived from software contributed to Berkeley by
Packit c04fcb
 * Chris Torek.
Packit c04fcb
 *
Packit c04fcb
 * Redistribution and use in source and binary forms, with or without
Packit c04fcb
 * modification, are permitted provided that the following conditions
Packit c04fcb
 * are met:
Packit c04fcb
 * 1. Redistributions of source code must retain the above copyright
Packit c04fcb
 *    notice, this list of conditions and the following disclaimer.
Packit c04fcb
 * 2. Redistributions in binary form must reproduce the above copyright
Packit c04fcb
 *    notice, this list of conditions and the following disclaimer in the
Packit c04fcb
 *    documentation and/or other materials provided with the distribution.
Packit c04fcb
 * 3. Neither the name of the University nor the names of its contributors
Packit c04fcb
 *    may be used to endorse or promote products derived from this software
Packit c04fcb
 *    without specific prior written permission.
Packit c04fcb
 *
Packit c04fcb
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit c04fcb
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit c04fcb
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit c04fcb
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit c04fcb
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit c04fcb
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit c04fcb
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit c04fcb
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit c04fcb
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit c04fcb
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit c04fcb
 * SUCH DAMAGE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <limits.h>
Packit c04fcb
#include <stdio.h>
Packit c04fcb
#include <string.h>
Packit c04fcb
#include <stdarg.h>
Packit c04fcb
#include "local.h"
Packit c04fcb
#include "ust_snprintf.h"
Packit c04fcb
Packit c04fcb
#define DUMMY_LEN	1
Packit c04fcb
Packit c04fcb
int ust_safe_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
Packit c04fcb
{
Packit c04fcb
	int ret;
Packit c04fcb
	char dummy[DUMMY_LEN];
Packit c04fcb
	LTTNG_UST_LFILE f;
Packit c04fcb
	struct __lttng_ust_sfileext fext;
Packit c04fcb
Packit c04fcb
	/* While snprintf(3) specifies size_t stdio uses an int internally */
Packit c04fcb
	if (n > INT_MAX)
Packit c04fcb
		n = INT_MAX;
Packit c04fcb
	/* Stdio internals do not deal correctly with zero length buffer */
Packit c04fcb
	if (n == 0) {
Packit c04fcb
		str = dummy;
Packit c04fcb
		n = DUMMY_LEN;
Packit c04fcb
	}
Packit c04fcb
	_FILEEXT_SETUP(&f, &fext);
Packit c04fcb
	f._file = -1;
Packit c04fcb
	f._flags = __SWR | __SSTR;
Packit c04fcb
	f._bf._base = f._p = (unsigned char *)str;
Packit c04fcb
	f._bf._size = f._w = n - 1;
Packit c04fcb
	ret = ust_safe_vfprintf(&f, fmt, ap);
Packit c04fcb
	*f._p = '\0';
Packit c04fcb
	return (ret);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
Packit c04fcb
{
Packit c04fcb
	va_list ap;
Packit c04fcb
	int ret;
Packit c04fcb
Packit c04fcb
	va_start(ap, fmt);
Packit c04fcb
	ret = ust_safe_vsnprintf(str, n, fmt, ap);
Packit c04fcb
	va_end(ap);
Packit c04fcb
Packit c04fcb
	return ret;
Packit c04fcb
}