sprintf-chk.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Checking sprintf.
  2. Copyright (C) 2005, 2009 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. In addition to the permissions in the GNU General Public License, the
  9. Free Software Foundation gives you unlimited permission to link the
  10. compiled version of this file into combinations with other programs,
  11. and to distribute those combinations without any restriction coming
  12. from the use of this file. (The General Public License restrictions
  13. do apply in other respects; for example, they cover modification of
  14. the file, and distribution when not linked into a combine
  15. executable.)
  16. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  17. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. for more details.
  20. Under Section 7 of GPL version 3, you are granted additional
  21. permissions described in the GCC Runtime Library Exception, version
  22. 3.1, as published by the Free Software Foundation.
  23. You should have received a copy of the GNU General Public License and
  24. a copy of the GCC Runtime Library Exception along with this program;
  25. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  26. <http://www.gnu.org/licenses/>. */
  27. #include "config.h"
  28. #include <ssp/ssp.h>
  29. #include <stdarg.h>
  30. #ifdef HAVE_LIMITS_H
  31. # include <limits.h>
  32. #endif
  33. #ifdef HAVE_STDIO_H
  34. # include <stdio.h>
  35. #endif
  36. extern void __chk_fail (void) __attribute__((__noreturn__));
  37. #ifdef HAVE_USABLE_VSNPRINTF
  38. int
  39. __sprintf_chk (char *s, int flags __attribute__((unused)),
  40. size_t slen, const char *format, ...)
  41. {
  42. va_list arg;
  43. int done;
  44. va_start (arg, format);
  45. if (slen > (size_t) INT_MAX)
  46. done = vsprintf (s, format, arg);
  47. else
  48. {
  49. done = vsnprintf (s, slen, format, arg);
  50. if (done >= 0 && (size_t) done >= slen)
  51. __chk_fail ();
  52. }
  53. va_end (arg);
  54. return done;
  55. }
  56. #endif