strlcpy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*------------------------------------------------------------------
  2. *
  3. * Module: strlcpy.c
  4. *
  5. * Purpose: Safe string functions to guard against buffer overflow.
  6. *
  7. * Description: The size of character strings, especially when coming from the
  8. * outside, can sometimes exceed a fixed size storage area.
  9. *
  10. * There was one case where a MIC-E format packet had an enormous
  11. * comment that exceeded an internal buffer of 256 characters,
  12. * resulting in a crash.
  13. *
  14. * We are not always meticulous about checking sizes to avoid overflow.
  15. * Use of these functions, instead of strcpy and strcat, should
  16. * help avoid issues.
  17. *
  18. * Orgin: From OpenBSD as the copyright notice indicates.
  19. * The GNU folks didn't think it was appropriate for inclusion
  20. * in glibc. https://lwn.net/Articles/507319/
  21. *
  22. * Modifications: Added extra debug output when strings are truncated.
  23. * Not sure if I will leave this in the release version
  24. * or just let it happen silently.
  25. *
  26. *---------------------------------------------------------------*/
  27. /* $NetBSD: strlcpy.c,v 1.5 2014/10/31 18:59:32 spz Exp $ */
  28. /* from NetBSD: strlcpy.c,v 1.14 2003/10/27 00:12:42 lukem Exp */
  29. /* from OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp */
  30. /*
  31. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  32. *
  33. * Permission to use, copy, modify, and distribute this software for any
  34. * purpose with or without fee is hereby granted, provided that the above
  35. * copyright notice and this permission notice appear in all copies.
  36. *
  37. * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
  38. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
  40. * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  41. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  42. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  43. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44. */
  45. #include "direwolf.h"
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <stdio.h>
  49. //#include "textcolor.h"
  50. /*
  51. * Copy src to string dst of size siz. At most siz-1 characters
  52. * will be copied. Always NUL terminates (unless siz == 0).
  53. * Returns strlen(src); if retval >= siz, truncation occurred.
  54. */
  55. #if DEBUG_STRL
  56. size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line)
  57. #else
  58. size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz)
  59. #endif
  60. {
  61. char *d = dst;
  62. const char *s = src;
  63. size_t n = siz;
  64. size_t retval;
  65. #if DEBUG_STRL
  66. if (dst == NULL) {
  67. //text_color_set (DW_COLOR_ERROR);
  68. printf ("ERROR: strlcpy dst is NULL. (%s %s %d)\n", file, func, line);
  69. return (0);
  70. }
  71. if (src == NULL) {
  72. //text_color_set (DW_COLOR_ERROR);
  73. printf ("ERROR: strlcpy src is NULL. (%s %s %d)\n", file, func, line);
  74. return (0);
  75. }
  76. if (siz == 1 || siz == 4) {
  77. //text_color_set (DW_COLOR_ERROR);
  78. printf ("Suspicious strlcpy siz. Is it using sizeof a pointer variable? (%s %s %d)\n", file, func, line);
  79. }
  80. #endif
  81. /* Copy as many bytes as will fit */
  82. if (n != 0 && --n != 0) {
  83. do {
  84. if ((*d++ = *s++) == 0)
  85. break;
  86. } while (--n != 0);
  87. }
  88. /* Not enough room in dst, add NUL and traverse rest of src */
  89. if (n == 0) {
  90. if (siz != 0)
  91. *d = '\0'; /* NUL-terminate dst */
  92. while (*s++)
  93. ;
  94. }
  95. retval = s - src - 1; /* count does not include NUL */
  96. #if DEBUG_STRL
  97. if (retval >= siz) {
  98. //text_color_set (DW_COLOR_ERROR);
  99. printf ("WARNING: strlcpy result length %d exceeds maximum length %d. (%s %s %d)\n",
  100. (int)retval, (int)(siz-1), file, func, line);
  101. }
  102. #endif
  103. return (retval);
  104. }