tst-sprintf-ub.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Test the sprintf (buf, "%s", buf) does not override buf.
  2. Copyright (C) 2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <support/check.h>
  19. enum
  20. {
  21. FUNCTION_FIRST,
  22. FUNCTION_SPRINTF = FUNCTION_FIRST,
  23. FUNCTION_SNPRINF,
  24. FUNCTION_VSPRINTF,
  25. FUNCTION_VSNPRINTF,
  26. FUNCTION_LAST
  27. };
  28. static void
  29. do_one_test (int function, char *buf, ...)
  30. {
  31. va_list args;
  32. char *arg;
  33. /* Expected results for fortified and non-fortified sprintf. */
  34. #if defined _FORTIFY_SOURCE && _FORTIFY_SOURCE > 1
  35. const char *expected = "CD";
  36. #else
  37. const char *expected = "ABCD";
  38. #endif
  39. va_start (args, buf);
  40. arg = va_arg (args, char *);
  41. va_end (args);
  42. switch (function)
  43. {
  44. /* The regular sprintf and vsprintf functions do not override the
  45. destination buffer, even if source and destination overlap. */
  46. case FUNCTION_SPRINTF:
  47. sprintf (buf, "%sCD", buf);
  48. TEST_COMPARE_STRING (buf, expected);
  49. break;
  50. case FUNCTION_VSPRINTF:
  51. va_start (args, buf);
  52. vsprintf (arg, "%sCD", args);
  53. TEST_COMPARE_STRING (arg, expected);
  54. va_end (args);
  55. break;
  56. /* On the other hand, snprint and vsnprint override overlapping
  57. source and destination buffers. */
  58. case FUNCTION_SNPRINF:
  59. snprintf (buf, 3, "%sCD", buf);
  60. TEST_COMPARE_STRING (buf, "CD");
  61. break;
  62. case FUNCTION_VSNPRINTF:
  63. va_start (args, buf);
  64. vsnprintf (arg, 3, "%sCD", args);
  65. TEST_COMPARE_STRING (arg, "CD");
  66. va_end (args);
  67. break;
  68. default:
  69. support_record_failure ();
  70. }
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. char buf[8];
  76. int i;
  77. /* For each function in the enum do:
  78. - reset the buffer to the initial state "AB";
  79. - call the function with buffer as source and destination;
  80. - check for the desired behavior. */
  81. for (i = FUNCTION_FIRST; i < FUNCTION_LAST; i++)
  82. {
  83. strncpy (buf, "AB", 3);
  84. do_one_test (i, buf, buf);
  85. }
  86. return 0;
  87. }
  88. #include <support/test-driver.c>