xref: /netbsd-src/external/gpl3/gcc/dist/libssp/vsprintf-chk.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg /* Checking vsprintf.
2*b1e83836Smrg    Copyright (C) 2005-2022 Free Software Foundation, Inc.
34fee23f9Smrg 
44fee23f9Smrg This file is part of GCC.
54fee23f9Smrg 
64fee23f9Smrg GCC is free software; you can redistribute it and/or modify it under
74fee23f9Smrg the terms of the GNU General Public License as published by the Free
84fee23f9Smrg Software Foundation; either version 3, or (at your option) any later
94fee23f9Smrg version.
104fee23f9Smrg 
114fee23f9Smrg In addition to the permissions in the GNU General Public License, the
124fee23f9Smrg Free Software Foundation gives you unlimited permission to link the
134fee23f9Smrg compiled version of this file into combinations with other programs,
144fee23f9Smrg and to distribute those combinations without any restriction coming
154fee23f9Smrg from the use of this file.  (The General Public License restrictions
164fee23f9Smrg do apply in other respects; for example, they cover modification of
174fee23f9Smrg the file, and distribution when not linked into a combine
184fee23f9Smrg executable.)
194fee23f9Smrg 
204fee23f9Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
214fee23f9Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
224fee23f9Smrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
234fee23f9Smrg for more details.
244fee23f9Smrg 
254fee23f9Smrg Under Section 7 of GPL version 3, you are granted additional
264fee23f9Smrg permissions described in the GCC Runtime Library Exception, version
274fee23f9Smrg 3.1, as published by the Free Software Foundation.
284fee23f9Smrg 
294fee23f9Smrg You should have received a copy of the GNU General Public License and
304fee23f9Smrg a copy of the GCC Runtime Library Exception along with this program;
314fee23f9Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
324fee23f9Smrg <http://www.gnu.org/licenses/>.  */
334fee23f9Smrg 
344fee23f9Smrg #include "config.h"
354fee23f9Smrg #include <ssp/ssp.h>
364fee23f9Smrg #include <stdarg.h>
374fee23f9Smrg #ifdef HAVE_LIMITS_H
384fee23f9Smrg # include <limits.h>
394fee23f9Smrg #endif
404fee23f9Smrg #ifdef HAVE_STDIO_H
414fee23f9Smrg # include <stdio.h>
424fee23f9Smrg #endif
434fee23f9Smrg 
444fee23f9Smrg extern void __chk_fail (void) __attribute__((__noreturn__));
454fee23f9Smrg 
464fee23f9Smrg #ifdef HAVE_USABLE_VSNPRINTF
474fee23f9Smrg int
__vsprintf_chk(char * s,int flags,size_t slen,const char * format,va_list arg)484fee23f9Smrg __vsprintf_chk (char *s, int flags __attribute__((unused)),
494fee23f9Smrg 		size_t slen, const char *format, va_list arg)
504fee23f9Smrg {
514fee23f9Smrg   int done;
524fee23f9Smrg 
534fee23f9Smrg   if (slen > (size_t) INT_MAX)
544fee23f9Smrg     done = vsprintf (s, format, arg);
554fee23f9Smrg   else
564fee23f9Smrg     {
574fee23f9Smrg       done = vsnprintf (s, slen, format, arg);
584fee23f9Smrg       if (done >= 0 && (size_t) done >= slen)
594fee23f9Smrg 	__chk_fail ();
604fee23f9Smrg     }
614fee23f9Smrg   return done;
624fee23f9Smrg }
634fee23f9Smrg #endif
64