010-backport_sscanf_alloc.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. From 8cfb43de636faa401634340d1a18404844f9ba5a Mon Sep 17 00:00:00 2001
  2. From: Mike Frysinger <vapier@gentoo.org>
  3. Date: Sun, 6 May 2012 03:50:44 -0400
  4. Subject: [PATCH] stdio: implement assignment-allocation "m" character
  5. The latest POSIX spec introduces a "m" character to allocate buffers for
  6. the user when using scanf type functions. This is like the old glibc "a"
  7. flag, but now standardized. With packages starting to use these, we need
  8. to implement it.
  9. for example:
  10. char *s;
  11. sscanf("foo", "%ms", &s);
  12. printf("%s\n", s);
  13. free(s);
  14. This will automatically allocate storage for "s", read in "foo" to it,
  15. and then display it.
  16. I'm not terribly familiar with the stdio layer, so this could be wrong.
  17. But it seems to work for me.
  18. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
  19. Signed-off-by: Felix Fietkau <nbd@openwrt.org>
  20. ---
  21. extra/Configs/Config.in | 13 ----------
  22. libc/stdio/_scanf.c | 68 ++++++++++++++++++++++++++++---------------------
  23. 2 files changed, 39 insertions(+), 42 deletions(-)
  24. --- a/extra/Configs/Config.in
  25. +++ b/extra/Configs/Config.in
  26. @@ -1590,19 +1590,6 @@ config UCLIBC_PRINTF_SCANF_POSITIONAL_AR
  27. Most people will answer 9.
  28. -
  29. -config UCLIBC_HAS_SCANF_GLIBC_A_FLAG
  30. - bool "Support glibc's 'a' flag for scanf string conversions (not implemented)"
  31. - help
  32. - NOTE!!! Currently Not Implemented!!! Just A Place Holder!! NOTE!!!
  33. - NOTE!!! Conflicts with an ANSI/ISO C99 scanf flag!! NOTE!!!
  34. -
  35. - Answer Y to enable support for glibc's 'a' flag for the scanf string
  36. - conversions '%s', '%[', '%ls', '%l[', and '%S'. This is used to
  37. - auto-allocate sufficient memory to hold the data retrieved.
  38. -
  39. - Most people will answer N.
  40. -
  41. choice
  42. prompt "Stdio buffer size"
  43. default UCLIBC_HAS_STDIO_BUFSIZ_4096
  44. --- a/libc/stdio/_scanf.c
  45. +++ b/libc/stdio/_scanf.c
  46. @@ -77,14 +77,6 @@
  47. #include <bits/uClibc_fpmax.h>
  48. #endif /* __UCLIBC_HAS_FLOATS__ */
  49. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  50. -#ifdef L_vfscanf
  51. -/* only emit this once */
  52. -#warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
  53. -#endif
  54. -#undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  55. -#endif
  56. -
  57. #undef __STDIO_HAS_VSSCANF
  58. #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  59. #define __STDIO_HAS_VSSCANF 1
  60. @@ -433,8 +425,9 @@ libc_hidden_def(vswscanf)
  61. /* float layout 0123456789012345678901 repeat n for "l[" */
  62. -#define SPEC_CHARS "npxXoudifFeEgGaACSncs["
  63. -/* npxXoudif eEgG CS cs[ */
  64. +#define SPEC_CHARS "npxXoudifFeEgGaACSnmcs["
  65. +/* npxXoudif eEgG CS cs[ */
  66. +/* NOTE: the 'm' flag must come before any convs that support it */
  67. /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
  68. * must immediately precede CONV_c. */
  69. @@ -444,7 +437,7 @@ enum {
  70. CONV_p,
  71. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  72. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  73. - CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
  74. + CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_m, CONV_c, CONV_s, CONV_leftbracket,
  75. CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
  76. };
  77. @@ -474,7 +467,7 @@ enum {
  78. FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
  79. FLAG_THOUSANDS = 0x20,
  80. FLAG_I18N = 0x40, /* only works for d, i, u */
  81. - FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
  82. + FLAG_MALLOC = 0x80, /* only works for c, s, S, and [ (and l[)*/
  83. };
  84. @@ -491,7 +484,7 @@ enum {
  85. /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  86. /* C */ ( 0|FLAG_SURPRESS), \
  87. /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
  88. - /* c */ (0x04|FLAG_SURPRESS), \
  89. + /* c */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  90. /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  91. }
  92. @@ -904,17 +897,17 @@ int attribute_hidden __psfs_parse_spec(r
  93. if (*psfs->fmt == *p) {
  94. int p_m_spec_chars = p - spec_chars;
  95. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  96. -#error implement gnu a flag
  97. - if ((*p == 'a')
  98. - && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
  99. - ) { /* Assumes ascii for 's' and 'S' test. */
  100. - psfs->flags |= FLAG_MALLOC;
  101. + if (*p == 'm' &&
  102. + (psfs->fmt[1] == '[' || psfs->fmt[1] == 'c' ||
  103. + /* Assumes ascii for 's' and 'S' test. */
  104. + (psfs->fmt[1] | 0x20) == 's'))
  105. + {
  106. + if (psfs->store)
  107. + psfs->flags |= FLAG_MALLOC;
  108. ++psfs->fmt;
  109. ++p;
  110. - continue; /* The related conversions follow 'a'. */
  111. + continue; /* The related conversions follow 'm'. */
  112. }
  113. -#endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  114. for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
  115. if (((psfs->dataargtype >> 8) | psfs->flags)
  116. @@ -1265,12 +1258,6 @@ int VFSCANF (FILE *__restrict fp, const
  117. while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
  118. *b++ = *wf++;
  119. }
  120. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  121. -#error this is wrong... we need to ched in __psfs_parse_spec instead since this checks last char in buffer and conversion my have stopped before it.
  122. - if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
  123. - goto DONE; /* Spec was excessively long. */
  124. - }
  125. -#endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  126. *b = 0;
  127. if (b == buf) { /* Bad conversion specifier! */
  128. goto DONE;
  129. @@ -1390,13 +1377,36 @@ int VFSCANF (FILE *__restrict fp, const
  130. }
  131. if (psfs.conv_num == CONV_s) {
  132. + /* We might have to handle the allocation ourselves */
  133. + int len;
  134. + /* With 'm', we actually got a pointer to a pointer */
  135. + unsigned char **ptr = (void *)b;
  136. +
  137. + i = 0;
  138. + if (psfs.flags & FLAG_MALLOC) {
  139. + len = 0;
  140. + b = NULL;
  141. + } else
  142. + len = -1;
  143. +
  144. /* Yes, believe it or not, a %s conversion can store nuls. */
  145. while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
  146. zero_conversions = 0;
  147. - *b = sc.cc;
  148. - b += psfs.store;
  149. + if (i == len) {
  150. + /* Pick a size that won't trigger a lot of
  151. + * mallocs early on ... */
  152. + len += 256;
  153. + b = realloc(b, len + 1);
  154. + }
  155. + b[i] = sc.cc;
  156. + i += psfs.store;
  157. fail = 0;
  158. }
  159. +
  160. + if (psfs.flags & FLAG_MALLOC)
  161. + *ptr = b;
  162. + /* The code below takes care of terminating NUL */
  163. + b += i;
  164. } else {
  165. #ifdef __UCLIBC_HAS_WCHAR__
  166. assert((psfs.conv_num == CONV_LEFTBRACKET) || \