0002-Rewrite-dynamic-string-support.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. From dd96882877721703e19272fe25034560b794061b Mon Sep 17 00:00:00 2001
  2. From: Sergey Poznyakoff <gray@gnu.org>
  3. Date: Sat, 7 Aug 2021 12:52:21 +0300
  4. Subject: Rewrite dynamic string support.
  5. * src/dstring.c (ds_init): Take a single argument.
  6. (ds_free): New function.
  7. (ds_resize): Take a single argument. Use x2nrealloc to expand
  8. the storage.
  9. (ds_reset,ds_append,ds_concat,ds_endswith): New function.
  10. (ds_fgetstr): Rewrite. In particular, this fixes integer overflow.
  11. * src/dstring.h (dynamic_string): Keep both the allocated length
  12. (ds_size) and index of the next free byte in the string (ds_idx).
  13. (ds_init,ds_resize): Change signature.
  14. (ds_len): New macro.
  15. (ds_free,ds_reset,ds_append,ds_concat,ds_endswith): New protos.
  16. * src/copyin.c: Use new ds_ functions.
  17. * src/copyout.c: Likewise.
  18. * src/copypass.c: Likewise.
  19. * src/util.c: Likewise.
  20. [Retrieved from:
  21. https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=dd96882877721703e19272fe25034560b794061b]
  22. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  23. ---
  24. src/copyin.c | 40 +++++++++++++-------------
  25. src/copyout.c | 16 ++++-------
  26. src/copypass.c | 34 +++++++++++------------
  27. src/dstring.c | 88 ++++++++++++++++++++++++++++++++++++++++++----------------
  28. src/dstring.h | 31 ++++++++++-----------
  29. src/util.c | 6 ++--
  30. 6 files changed, 123 insertions(+), 92 deletions(-)
  31. diff --git a/src/copyin.c b/src/copyin.c
  32. index a096048..4fb14af 100644
  33. --- a/src/copyin.c
  34. +++ b/src/copyin.c
  35. @@ -55,11 +55,12 @@ query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
  36. char *str_res; /* Result for string function. */
  37. static dynamic_string new_name; /* New file name for rename option. */
  38. static int initialized_new_name = false;
  39. +
  40. if (!initialized_new_name)
  41. - {
  42. - ds_init (&new_name, 128);
  43. - initialized_new_name = true;
  44. - }
  45. + {
  46. + ds_init (&new_name);
  47. + initialized_new_name = true;
  48. + }
  49. if (rename_flag)
  50. {
  51. @@ -780,37 +781,36 @@ long_format (struct cpio_file_stat *file_hdr, char const *link_name)
  52. already in `save_patterns' (from the command line) are preserved. */
  53. static void
  54. -read_pattern_file ()
  55. +read_pattern_file (void)
  56. {
  57. - int max_new_patterns;
  58. - char **new_save_patterns;
  59. - int new_num_patterns;
  60. + char **new_save_patterns = NULL;
  61. + size_t max_new_patterns;
  62. + size_t new_num_patterns;
  63. int i;
  64. - dynamic_string pattern_name;
  65. + dynamic_string pattern_name = DYNAMIC_STRING_INITIALIZER;
  66. FILE *pattern_fp;
  67. if (num_patterns < 0)
  68. num_patterns = 0;
  69. - max_new_patterns = 1 + num_patterns;
  70. - new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
  71. new_num_patterns = num_patterns;
  72. - ds_init (&pattern_name, 128);
  73. + max_new_patterns = num_patterns;
  74. + new_save_patterns = xcalloc (max_new_patterns, sizeof (new_save_patterns[0]));
  75. pattern_fp = fopen (pattern_file_name, "r");
  76. if (pattern_fp == NULL)
  77. open_fatal (pattern_file_name);
  78. while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
  79. {
  80. - if (new_num_patterns >= max_new_patterns)
  81. - {
  82. - max_new_patterns += 1;
  83. - new_save_patterns = (char **)
  84. - xrealloc ((char *) new_save_patterns,
  85. - max_new_patterns * sizeof (char *));
  86. - }
  87. + if (new_num_patterns == max_new_patterns)
  88. + new_save_patterns = x2nrealloc (new_save_patterns,
  89. + &max_new_patterns,
  90. + sizeof (new_save_patterns[0]));
  91. new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
  92. ++new_num_patterns;
  93. }
  94. +
  95. + ds_free (&pattern_name);
  96. +
  97. if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
  98. close_error (pattern_file_name);
  99. @@ -1210,7 +1210,7 @@ swab_array (char *ptr, int count)
  100. in the file system. */
  101. void
  102. -process_copy_in ()
  103. +process_copy_in (void)
  104. {
  105. FILE *tty_in = NULL; /* Interactive file for rename option. */
  106. FILE *tty_out = NULL; /* Interactive file for rename option. */
  107. diff --git a/src/copyout.c b/src/copyout.c
  108. index 5ca587f..ca6798c 100644
  109. --- a/src/copyout.c
  110. +++ b/src/copyout.c
  111. @@ -594,9 +594,10 @@ assign_string (char **pvar, char *value)
  112. The format of the header depends on the compatibility (-c) flag. */
  113. void
  114. -process_copy_out ()
  115. +process_copy_out (void)
  116. {
  117. - dynamic_string input_name; /* Name of file read from stdin. */
  118. + dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
  119. + /* Name of file read from stdin. */
  120. struct stat file_stat; /* Stat record for file. */
  121. struct cpio_file_stat file_hdr = CPIO_FILE_STAT_INITIALIZER;
  122. /* Output header information. */
  123. @@ -605,7 +606,6 @@ process_copy_out ()
  124. char *orig_file_name = NULL;
  125. /* Initialize the copy out. */
  126. - ds_init (&input_name, 128);
  127. file_hdr.c_magic = 070707;
  128. /* Check whether the output file might be a tape. */
  129. @@ -657,14 +657,9 @@ process_copy_out ()
  130. {
  131. if (file_hdr.c_mode & CP_IFDIR)
  132. {
  133. - int len = strlen (input_name.ds_string);
  134. /* Make sure the name ends with a slash */
  135. - if (input_name.ds_string[len-1] != '/')
  136. - {
  137. - ds_resize (&input_name, len + 2);
  138. - input_name.ds_string[len] = '/';
  139. - input_name.ds_string[len+1] = 0;
  140. - }
  141. + if (!ds_endswith (&input_name, '/'))
  142. + ds_append (&input_name, '/');
  143. }
  144. }
  145. @@ -875,6 +870,7 @@ process_copy_out ()
  146. (unsigned long) blocks), (unsigned long) blocks);
  147. }
  148. cpio_file_stat_free (&file_hdr);
  149. + ds_free (&input_name);
  150. }
  151. diff --git a/src/copypass.c b/src/copypass.c
  152. index 5d5e939..23ee687 100644
  153. --- a/src/copypass.c
  154. +++ b/src/copypass.c
  155. @@ -48,10 +48,12 @@ set_copypass_perms (int fd, const char *name, struct stat *st)
  156. If `link_flag', link instead of copying. */
  157. void
  158. -process_copy_pass ()
  159. +process_copy_pass (void)
  160. {
  161. - dynamic_string input_name; /* Name of file from stdin. */
  162. - dynamic_string output_name; /* Name of new file. */
  163. + dynamic_string input_name = DYNAMIC_STRING_INITIALIZER;
  164. + /* Name of file from stdin. */
  165. + dynamic_string output_name = DYNAMIC_STRING_INITIALIZER;
  166. + /* Name of new file. */
  167. size_t dirname_len; /* Length of `directory_name'. */
  168. int res; /* Result of functions. */
  169. char *slash; /* For moving past slashes in input name. */
  170. @@ -65,25 +67,18 @@ process_copy_pass ()
  171. created files */
  172. /* Initialize the copy pass. */
  173. - ds_init (&input_name, 128);
  174. dirname_len = strlen (directory_name);
  175. if (change_directory_option && !ISSLASH (directory_name[0]))
  176. {
  177. char *pwd = xgetcwd ();
  178. -
  179. - dirname_len += strlen (pwd) + 1;
  180. - ds_init (&output_name, dirname_len + 2);
  181. - strcpy (output_name.ds_string, pwd);
  182. - strcat (output_name.ds_string, "/");
  183. - strcat (output_name.ds_string, directory_name);
  184. +
  185. + ds_concat (&output_name, pwd);
  186. + ds_append (&output_name, '/');
  187. }
  188. - else
  189. - {
  190. - ds_init (&output_name, dirname_len + 2);
  191. - strcpy (output_name.ds_string, directory_name);
  192. - }
  193. - output_name.ds_string[dirname_len] = '/';
  194. + ds_concat (&output_name, directory_name);
  195. + ds_append (&output_name, '/');
  196. + dirname_len = ds_len (&output_name);
  197. output_is_seekable = true;
  198. change_dir ();
  199. @@ -116,8 +111,8 @@ process_copy_pass ()
  200. /* Make the name of the new file. */
  201. for (slash = input_name.ds_string; *slash == '/'; ++slash)
  202. ;
  203. - ds_resize (&output_name, dirname_len + strlen (slash) + 2);
  204. - strcpy (output_name.ds_string + dirname_len + 1, slash);
  205. + ds_reset (&output_name, dirname_len);
  206. + ds_concat (&output_name, slash);
  207. existing_dir = false;
  208. if (lstat (output_name.ds_string, &out_file_stat) == 0)
  209. @@ -333,6 +328,9 @@ process_copy_pass ()
  210. (unsigned long) blocks),
  211. (unsigned long) blocks);
  212. }
  213. +
  214. + ds_free (&input_name);
  215. + ds_free (&output_name);
  216. }
  217. /* Try and create a hard link from FILE_NAME to another file
  218. diff --git a/src/dstring.c b/src/dstring.c
  219. index b261d5a..692d3e7 100644
  220. --- a/src/dstring.c
  221. +++ b/src/dstring.c
  222. @@ -20,8 +20,8 @@
  223. #if defined(HAVE_CONFIG_H)
  224. # include <config.h>
  225. #endif
  226. -
  227. #include <stdio.h>
  228. +#include <stdlib.h>
  229. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  230. #include <string.h>
  231. #else
  232. @@ -33,24 +33,41 @@
  233. /* Initialiaze dynamic string STRING with space for SIZE characters. */
  234. void
  235. -ds_init (dynamic_string *string, int size)
  236. +ds_init (dynamic_string *string)
  237. +{
  238. + memset (string, 0, sizeof *string);
  239. +}
  240. +
  241. +/* Free the dynamic string storage. */
  242. +
  243. +void
  244. +ds_free (dynamic_string *string)
  245. {
  246. - string->ds_length = size;
  247. - string->ds_string = (char *) xmalloc (size);
  248. + free (string->ds_string);
  249. }
  250. -/* Expand dynamic string STRING, if necessary, to hold SIZE characters. */
  251. +/* Expand dynamic string STRING, if necessary. */
  252. void
  253. -ds_resize (dynamic_string *string, int size)
  254. +ds_resize (dynamic_string *string)
  255. {
  256. - if (size > string->ds_length)
  257. + if (string->ds_idx == string->ds_size)
  258. {
  259. - string->ds_length = size;
  260. - string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
  261. + string->ds_string = x2nrealloc (string->ds_string, &string->ds_size,
  262. + 1);
  263. }
  264. }
  265. +/* Reset the index of the dynamic string S to LEN. */
  266. +
  267. +void
  268. +ds_reset (dynamic_string *s, size_t len)
  269. +{
  270. + while (len > s->ds_size)
  271. + ds_resize (s);
  272. + s->ds_idx = len;
  273. +}
  274. +
  275. /* Dynamic string S gets a string terminated by the EOS character
  276. (which is removed) from file F. S will increase
  277. in size during the function if the string from F is longer than
  278. @@ -61,34 +78,50 @@ ds_resize (dynamic_string *string, int size)
  279. char *
  280. ds_fgetstr (FILE *f, dynamic_string *s, char eos)
  281. {
  282. - int insize; /* Amount needed for line. */
  283. - int strsize; /* Amount allocated for S. */
  284. int next_ch;
  285. /* Initialize. */
  286. - insize = 0;
  287. - strsize = s->ds_length;
  288. + s->ds_idx = 0;
  289. /* Read the input string. */
  290. - next_ch = getc (f);
  291. - while (next_ch != eos && next_ch != EOF)
  292. + while ((next_ch = getc (f)) != eos && next_ch != EOF)
  293. {
  294. - if (insize >= strsize - 1)
  295. - {
  296. - ds_resize (s, strsize * 2 + 2);
  297. - strsize = s->ds_length;
  298. - }
  299. - s->ds_string[insize++] = next_ch;
  300. - next_ch = getc (f);
  301. + ds_resize (s);
  302. + s->ds_string[s->ds_idx++] = next_ch;
  303. }
  304. - s->ds_string[insize++] = '\0';
  305. + ds_resize (s);
  306. + s->ds_string[s->ds_idx] = '\0';
  307. - if (insize == 1 && next_ch == EOF)
  308. + if (s->ds_idx == 0 && next_ch == EOF)
  309. return NULL;
  310. else
  311. return s->ds_string;
  312. }
  313. +void
  314. +ds_append (dynamic_string *s, int c)
  315. +{
  316. + ds_resize (s);
  317. + s->ds_string[s->ds_idx] = c;
  318. + if (c)
  319. + {
  320. + s->ds_idx++;
  321. + ds_resize (s);
  322. + s->ds_string[s->ds_idx] = 0;
  323. + }
  324. +}
  325. +
  326. +void
  327. +ds_concat (dynamic_string *s, char const *str)
  328. +{
  329. + size_t len = strlen (str);
  330. + while (len + 1 > s->ds_size)
  331. + ds_resize (s);
  332. + memcpy (s->ds_string + s->ds_idx, str, len);
  333. + s->ds_idx += len;
  334. + s->ds_string[s->ds_idx] = 0;
  335. +}
  336. +
  337. char *
  338. ds_fgets (FILE *f, dynamic_string *s)
  339. {
  340. @@ -100,3 +133,10 @@ ds_fgetname (FILE *f, dynamic_string *s)
  341. {
  342. return ds_fgetstr (f, s, '\0');
  343. }
  344. +
  345. +/* Return true if the dynamic string S ends with character C. */
  346. +int
  347. +ds_endswith (dynamic_string *s, int c)
  348. +{
  349. + return (s->ds_idx > 0 && s->ds_string[s->ds_idx - 1] == c);
  350. +}
  351. diff --git a/src/dstring.h b/src/dstring.h
  352. index 5d24181..ca7a5f1 100644
  353. --- a/src/dstring.h
  354. +++ b/src/dstring.h
  355. @@ -17,10 +17,6 @@
  356. Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  357. Boston, MA 02110-1301 USA. */
  358. -#ifndef NULL
  359. -#define NULL 0
  360. -#endif
  361. -
  362. /* A dynamic string consists of record that records the size of an
  363. allocated string and the pointer to that string. The actual string
  364. is a normal zero byte terminated string that can be used with the
  365. @@ -30,22 +26,25 @@
  366. typedef struct
  367. {
  368. - int ds_length; /* Actual amount of storage allocated. */
  369. - char *ds_string; /* String. */
  370. + size_t ds_size; /* Actual amount of storage allocated. */
  371. + size_t ds_idx; /* Index of the next free byte in the string. */
  372. + char *ds_string; /* String storage. */
  373. } dynamic_string;
  374. +#define DYNAMIC_STRING_INITIALIZER { 0, 0, NULL }
  375. -/* Macros that look similar to the original string functions.
  376. - WARNING: These macros work only on pointers to dynamic string records.
  377. - If used with a real record, an "&" must be used to get the pointer. */
  378. -#define ds_strlen(s) strlen ((s)->ds_string)
  379. -#define ds_strcmp(s1, s2) strcmp ((s1)->ds_string, (s2)->ds_string)
  380. -#define ds_strncmp(s1, s2, n) strncmp ((s1)->ds_string, (s2)->ds_string, n)
  381. -#define ds_index(s, c) index ((s)->ds_string, c)
  382. -#define ds_rindex(s, c) rindex ((s)->ds_string, c)
  383. +void ds_init (dynamic_string *string);
  384. +void ds_free (dynamic_string *string);
  385. +void ds_reset (dynamic_string *s, size_t len);
  386. -void ds_init (dynamic_string *string, int size);
  387. -void ds_resize (dynamic_string *string, int size);
  388. +/* All functions below guarantee that s->ds_string[s->ds_idx] == '\0' */
  389. char *ds_fgetname (FILE *f, dynamic_string *s);
  390. char *ds_fgets (FILE *f, dynamic_string *s);
  391. char *ds_fgetstr (FILE *f, dynamic_string *s, char eos);
  392. +void ds_append (dynamic_string *s, int c);
  393. +void ds_concat (dynamic_string *s, char const *str);
  394. +
  395. +#define ds_len(s) ((s)->ds_idx)
  396. +
  397. +int ds_endswith (dynamic_string *s, int c);
  398. +
  399. diff --git a/src/util.c b/src/util.c
  400. index 996d4fa..ff2746d 100644
  401. --- a/src/util.c
  402. +++ b/src/util.c
  403. @@ -846,11 +846,9 @@ get_next_reel (int tape_des)
  404. FILE *tty_out; /* File for interacting with user. */
  405. int old_tape_des;
  406. char *next_archive_name;
  407. - dynamic_string new_name;
  408. + dynamic_string new_name = DYNAMIC_STRING_INITIALIZER;
  409. char *str_res;
  410. - ds_init (&new_name, 128);
  411. -
  412. /* Open files for interactive communication. */
  413. tty_in = fopen (TTY_NAME, "r");
  414. if (tty_in == NULL)
  415. @@ -925,7 +923,7 @@ get_next_reel (int tape_des)
  416. error (PAXEXIT_FAILURE, 0, _("internal error: tape descriptor changed from %d to %d"),
  417. old_tape_des, tape_des);
  418. - free (new_name.ds_string);
  419. + ds_free (&new_name);
  420. fclose (tty_in);
  421. fclose (tty_out);
  422. }
  423. --
  424. cgit v1.2.1