strcpy-chk.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Checking strcpy.
  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. #ifdef HAVE_STRING_H
  30. # include <string.h>
  31. #endif
  32. extern void __chk_fail (void) __attribute__((__noreturn__));
  33. char *
  34. __strcpy_chk (char *__restrict__ dest, const char *__restrict__ src,
  35. size_t slen)
  36. {
  37. size_t len = strlen (src);
  38. if (len >= slen)
  39. __chk_fail ();
  40. return memcpy (dest, src, len + 1);
  41. }