format.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* Parse a printf-style format string.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "format.h"
  16. format_pieces::format_pieces (const char **arg, bool gdb_extensions)
  17. {
  18. const char *s;
  19. const char *string;
  20. const char *prev_start;
  21. const char *percent_loc;
  22. char *sub_start, *current_substring;
  23. enum argclass this_argclass;
  24. s = *arg;
  25. if (gdb_extensions)
  26. {
  27. string = *arg;
  28. *arg += strlen (*arg);
  29. }
  30. else
  31. {
  32. /* Parse the format-control string and copy it into the string STRING,
  33. processing some kinds of escape sequence. */
  34. char *f = (char *) alloca (strlen (s) + 1);
  35. string = f;
  36. while ((gdb_extensions || *s != '"') && *s != '\0')
  37. {
  38. int c = *s++;
  39. switch (c)
  40. {
  41. case '\0':
  42. continue;
  43. case '\\':
  44. switch (c = *s++)
  45. {
  46. case '\\':
  47. *f++ = '\\';
  48. break;
  49. case 'a':
  50. *f++ = '\a';
  51. break;
  52. case 'b':
  53. *f++ = '\b';
  54. break;
  55. case 'e':
  56. *f++ = '\e';
  57. break;
  58. case 'f':
  59. *f++ = '\f';
  60. break;
  61. case 'n':
  62. *f++ = '\n';
  63. break;
  64. case 'r':
  65. *f++ = '\r';
  66. break;
  67. case 't':
  68. *f++ = '\t';
  69. break;
  70. case 'v':
  71. *f++ = '\v';
  72. break;
  73. case '"':
  74. *f++ = '"';
  75. break;
  76. default:
  77. /* ??? TODO: handle other escape sequences. */
  78. error (_("Unrecognized escape character \\%c in format string."),
  79. c);
  80. }
  81. break;
  82. default:
  83. *f++ = c;
  84. }
  85. }
  86. /* Terminate our escape-processed copy. */
  87. *f++ = '\0';
  88. /* Whether the format string ended with double-quote or zero, we're
  89. done with it; it's up to callers to complain about syntax. */
  90. *arg = s;
  91. }
  92. /* Need extra space for the '\0's. Doubling the size is sufficient. */
  93. current_substring = (char *) xmalloc (strlen (string) * 2 + 1000);
  94. m_storage.reset (current_substring);
  95. /* Now scan the string for %-specs and see what kinds of args they want.
  96. argclass classifies the %-specs so we can give printf-type functions
  97. something of the right size. */
  98. const char *f = string;
  99. prev_start = string;
  100. while (*f)
  101. if (*f++ == '%')
  102. {
  103. int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
  104. int seen_space = 0, seen_plus = 0;
  105. int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
  106. int seen_big_d = 0, seen_double_big_d = 0;
  107. int seen_size_t = 0;
  108. int bad = 0;
  109. int n_int_args = 0;
  110. bool seen_i64 = false;
  111. /* Skip over "%%", it will become part of a literal piece. */
  112. if (*f == '%')
  113. {
  114. f++;
  115. continue;
  116. }
  117. sub_start = current_substring;
  118. strncpy (current_substring, prev_start, f - 1 - prev_start);
  119. current_substring += f - 1 - prev_start;
  120. *current_substring++ = '\0';
  121. if (*sub_start != '\0')
  122. m_pieces.emplace_back (sub_start, literal_piece, 0);
  123. percent_loc = f - 1;
  124. /* Check the validity of the format specifier, and work
  125. out what argument it expects. We only accept C89
  126. format strings, with the exception of long long (which
  127. we autoconf for). */
  128. /* The first part of a format specifier is a set of flag
  129. characters. */
  130. while (*f != '\0' && strchr ("0-+ #", *f))
  131. {
  132. if (*f == '#')
  133. seen_hash = 1;
  134. else if (*f == '0')
  135. seen_zero = 1;
  136. else if (*f == ' ')
  137. seen_space = 1;
  138. else if (*f == '+')
  139. seen_plus = 1;
  140. f++;
  141. }
  142. /* The next part of a format specifier is a width. */
  143. if (gdb_extensions && *f == '*')
  144. {
  145. ++f;
  146. ++n_int_args;
  147. }
  148. else
  149. {
  150. while (*f != '\0' && strchr ("0123456789", *f))
  151. f++;
  152. }
  153. /* The next part of a format specifier is a precision. */
  154. if (*f == '.')
  155. {
  156. seen_prec = 1;
  157. f++;
  158. if (gdb_extensions && *f == '*')
  159. {
  160. ++f;
  161. ++n_int_args;
  162. }
  163. else
  164. {
  165. while (*f != '\0' && strchr ("0123456789", *f))
  166. f++;
  167. }
  168. }
  169. /* The next part of a format specifier is a length modifier. */
  170. switch (*f)
  171. {
  172. case 'h':
  173. seen_h = 1;
  174. f++;
  175. break;
  176. case 'l':
  177. f++;
  178. lcount++;
  179. if (*f == 'l')
  180. {
  181. f++;
  182. lcount++;
  183. }
  184. break;
  185. case 'L':
  186. seen_big_l = 1;
  187. f++;
  188. break;
  189. case 'H':
  190. /* Decimal32 modifier. */
  191. seen_big_h = 1;
  192. f++;
  193. break;
  194. case 'D':
  195. /* Decimal64 and Decimal128 modifiers. */
  196. f++;
  197. /* Check for a Decimal128. */
  198. if (*f == 'D')
  199. {
  200. f++;
  201. seen_double_big_d = 1;
  202. }
  203. else
  204. seen_big_d = 1;
  205. break;
  206. case 'z':
  207. /* For size_t or ssize_t. */
  208. seen_size_t = 1;
  209. f++;
  210. break;
  211. case 'I':
  212. /* Support the Windows '%I64' extension, because an
  213. earlier call to format_pieces might have converted %lld
  214. to %I64d. */
  215. if (f[1] == '6' && f[2] == '4')
  216. {
  217. f += 3;
  218. lcount = 2;
  219. seen_i64 = true;
  220. }
  221. break;
  222. }
  223. switch (*f)
  224. {
  225. case 'u':
  226. if (seen_hash)
  227. bad = 1;
  228. /* FALLTHROUGH */
  229. case 'o':
  230. case 'x':
  231. case 'X':
  232. if (seen_space || seen_plus)
  233. bad = 1;
  234. /* FALLTHROUGH */
  235. case 'd':
  236. case 'i':
  237. if (seen_size_t)
  238. this_argclass = size_t_arg;
  239. else if (lcount == 0)
  240. this_argclass = int_arg;
  241. else if (lcount == 1)
  242. this_argclass = long_arg;
  243. else
  244. this_argclass = long_long_arg;
  245. if (seen_big_l)
  246. bad = 1;
  247. break;
  248. case 'c':
  249. this_argclass = lcount == 0 ? int_arg : wide_char_arg;
  250. if (lcount > 1 || seen_h || seen_big_l)
  251. bad = 1;
  252. if (seen_prec || seen_zero || seen_space || seen_plus)
  253. bad = 1;
  254. break;
  255. case 'p':
  256. this_argclass = ptr_arg;
  257. if (lcount || seen_h || seen_big_l)
  258. bad = 1;
  259. if (seen_prec)
  260. bad = 1;
  261. if (seen_hash || seen_zero || seen_space || seen_plus)
  262. bad = 1;
  263. if (gdb_extensions)
  264. {
  265. switch (f[1])
  266. {
  267. case 's':
  268. case 'F':
  269. case '[':
  270. case ']':
  271. f++;
  272. break;
  273. }
  274. }
  275. break;
  276. case 's':
  277. this_argclass = lcount == 0 ? string_arg : wide_string_arg;
  278. if (lcount > 1 || seen_h || seen_big_l)
  279. bad = 1;
  280. if (seen_zero || seen_space || seen_plus)
  281. bad = 1;
  282. break;
  283. case 'e':
  284. case 'f':
  285. case 'g':
  286. case 'E':
  287. case 'G':
  288. if (seen_double_big_d)
  289. this_argclass = dec128float_arg;
  290. else if (seen_big_d)
  291. this_argclass = dec64float_arg;
  292. else if (seen_big_h)
  293. this_argclass = dec32float_arg;
  294. else if (seen_big_l)
  295. this_argclass = long_double_arg;
  296. else
  297. this_argclass = double_arg;
  298. if (lcount || seen_h)
  299. bad = 1;
  300. break;
  301. case '*':
  302. error (_("`*' not supported for precision or width in printf"));
  303. case 'n':
  304. error (_("Format specifier `n' not supported in printf"));
  305. case '\0':
  306. error (_("Incomplete format specifier at end of format string"));
  307. default:
  308. error (_("Unrecognized format specifier '%c' in printf"), *f);
  309. }
  310. if (bad)
  311. error (_("Inappropriate modifiers to "
  312. "format specifier '%c' in printf"),
  313. *f);
  314. f++;
  315. sub_start = current_substring;
  316. if (lcount > 1 && !seen_i64 && USE_PRINTF_I64)
  317. {
  318. /* Windows' printf does support long long, but not the usual way.
  319. Convert %lld to %I64d. */
  320. int length_before_ll = f - percent_loc - 1 - lcount;
  321. strncpy (current_substring, percent_loc, length_before_ll);
  322. strcpy (current_substring + length_before_ll, "I64");
  323. current_substring[length_before_ll + 3] =
  324. percent_loc[length_before_ll + lcount];
  325. current_substring += length_before_ll + 4;
  326. }
  327. else if (this_argclass == wide_string_arg
  328. || this_argclass == wide_char_arg)
  329. {
  330. /* Convert %ls or %lc to %s. */
  331. int length_before_ls = f - percent_loc - 2;
  332. strncpy (current_substring, percent_loc, length_before_ls);
  333. strcpy (current_substring + length_before_ls, "s");
  334. current_substring += length_before_ls + 2;
  335. }
  336. else
  337. {
  338. strncpy (current_substring, percent_loc, f - percent_loc);
  339. current_substring += f - percent_loc;
  340. }
  341. *current_substring++ = '\0';
  342. prev_start = f;
  343. m_pieces.emplace_back (sub_start, this_argclass, n_int_args);
  344. }
  345. /* Record the remainder of the string. */
  346. if (f > prev_start)
  347. {
  348. sub_start = current_substring;
  349. strncpy (current_substring, prev_start, f - prev_start);
  350. current_substring += f - prev_start;
  351. *current_substring++ = '\0';
  352. m_pieces.emplace_back (sub_start, literal_piece, 0);
  353. }
  354. }