0013-eliminate-global-var.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. From 1198be74dcd8982dfcc9ba5a05e8950376ea74af Mon Sep 17 00:00:00 2001
  2. From: Christos Zoulas <christos@zoulas.com>
  3. Date: Thu, 2 May 2013 21:58:20 +0000
  4. Subject: [PATCH] PR/248: Multiple magic_loads fail due to global address
  5. re-use. Add a struct to describe the set entries
  6. eliminating the global variable.
  7. ---
  8. src/apprentice.c | 58 ++++++++++++++++++++++++++++++------------------------
  9. 1 file changed, 32 insertions(+), 26 deletions(-)
  10. diff --git a/src/apprentice.c b/src/apprentice.c
  11. index 32ba2c0..aca977d 100644
  12. --- a/src/apprentice.c
  13. +++ b/src/apprentice.c
  14. @@ -80,6 +80,12 @@ struct magic_entry {
  15. uint32_t max_count;
  16. };
  17. +struct magic_entry_set {
  18. + struct magic_entry *me;
  19. + uint32_t count;
  20. + uint32_t max;
  21. +};
  22. +
  23. struct magic_map {
  24. void *p;
  25. size_t len;
  26. @@ -125,7 +131,6 @@ private int parse_strength(struct magic_set *, struct magic_entry *, const char
  27. private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
  28. -private size_t maxmagic[MAGIC_SETS] = { 0 };
  29. private size_t magicsize = sizeof(struct magic);
  30. private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
  31. @@ -897,24 +902,24 @@ set_test_type(struct magic *mstart, struct magic *m)
  32. private int
  33. addentry(struct magic_set *ms, struct magic_entry *me,
  34. - struct magic_entry **mentry, uint32_t *mentrycount)
  35. + struct magic_entry_set *mset)
  36. {
  37. size_t i = me->mp->type == FILE_NAME ? 1 : 0;
  38. - if (mentrycount[i] == maxmagic[i]) {
  39. + if (mset[i].count == mset[i].max) {
  40. struct magic_entry *mp;
  41. - maxmagic[i] += ALLOC_INCR;
  42. + mset[i].max += ALLOC_INCR;
  43. if ((mp = CAST(struct magic_entry *,
  44. - realloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
  45. + realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
  46. NULL) {
  47. - file_oomem(ms, sizeof(*mp) * maxmagic[i]);
  48. + file_oomem(ms, sizeof(*mp) * mset[i].max);
  49. return -1;
  50. }
  51. - (void)memset(&mp[mentrycount[i]], 0, sizeof(*mp) *
  52. + (void)memset(&mp[mset[i].count], 0, sizeof(*mp) *
  53. ALLOC_INCR);
  54. - mentry[i] = mp;
  55. + mset[i].me = mp;
  56. }
  57. - mentry[i][mentrycount[i]++] = *me;
  58. + mset[i].me[mset[i].count++] = *me;
  59. memset(me, 0, sizeof(*me));
  60. return 0;
  61. }
  62. @@ -924,7 +929,7 @@ addentry(struct magic_set *ms, struct magic_entry *me,
  63. */
  64. private void
  65. load_1(struct magic_set *ms, int action, const char *fn, int *errs,
  66. - struct magic_entry **mentry, uint32_t *mentrycount)
  67. + struct magic_entry_set *mset)
  68. {
  69. size_t lineno = 0, llen = 0;
  70. char *line = NULL;
  71. @@ -991,7 +996,7 @@ load_1(struct magic_set *ms, int action, const char *fn, int *errs,
  72. case 0:
  73. continue;
  74. case 1:
  75. - (void)addentry(ms, &me, mentry, mentrycount);
  76. + (void)addentry(ms, &me, mset);
  77. goto again;
  78. default:
  79. (*errs)++;
  80. @@ -1000,7 +1005,7 @@ load_1(struct magic_set *ms, int action, const char *fn, int *errs,
  81. }
  82. }
  83. if (me.mp)
  84. - (void)addentry(ms, &me, mentry, mentrycount);
  85. + (void)addentry(ms, &me, mset);
  86. free(line);
  87. (void)fclose(f);
  88. }
  89. @@ -1111,19 +1116,21 @@ private struct magic_map *
  90. apprentice_load(struct magic_set *ms, const char *fn, int action)
  91. {
  92. int errs = 0;
  93. - struct magic_entry *mentry[MAGIC_SETS] = { NULL };
  94. - uint32_t mentrycount[MAGIC_SETS] = { 0 };
  95. uint32_t i, j;
  96. size_t files = 0, maxfiles = 0;
  97. char **filearr = NULL, *mfn;
  98. struct stat st;
  99. struct magic_map *map;
  100. + struct magic_entry_set mset[MAGIC_SETS];
  101. DIR *dir;
  102. struct dirent *d;
  103. + memset(mset, 0, sizeof(mset));
  104. ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
  105. - if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
  106. +
  107. + if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
  108. + {
  109. file_oomem(ms, sizeof(*map));
  110. return NULL;
  111. }
  112. @@ -1169,36 +1176,35 @@ apprentice_load(struct magic_set *ms, const char *fn, int action)
  113. closedir(dir);
  114. qsort(filearr, files, sizeof(*filearr), cmpstrp);
  115. for (i = 0; i < files; i++) {
  116. - load_1(ms, action, filearr[i], &errs, mentry,
  117. - mentrycount);
  118. + load_1(ms, action, filearr[i], &errs, mset);
  119. free(filearr[i]);
  120. }
  121. free(filearr);
  122. } else
  123. - load_1(ms, action, fn, &errs, mentry, mentrycount);
  124. + load_1(ms, action, fn, &errs, mset);
  125. if (errs)
  126. goto out;
  127. for (j = 0; j < MAGIC_SETS; j++) {
  128. /* Set types of tests */
  129. - for (i = 0; i < mentrycount[j]; ) {
  130. - if (mentry[j][i].mp->cont_level != 0) {
  131. + for (i = 0; i < mset[j].count; ) {
  132. + if (mset[j].me[i].mp->cont_level != 0) {
  133. i++;
  134. continue;
  135. }
  136. - i = set_text_binary(ms, mentry[j], mentrycount[j], i);
  137. + i = set_text_binary(ms, mset[j].me, mset[j].count, i);
  138. }
  139. - qsort(mentry[j], mentrycount[j], sizeof(*mentry[j]),
  140. + qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me),
  141. apprentice_sort);
  142. /*
  143. * Make sure that any level 0 "default" line is last
  144. * (if one exists).
  145. */
  146. - set_last_default(ms, mentry[j], mentrycount[j]);
  147. + set_last_default(ms, mset[j].me, mset[j].count);
  148. /* coalesce per file arrays into a single one */
  149. - if (coalesce_entries(ms, mentry[j], mentrycount[j],
  150. + if (coalesce_entries(ms, mset[j].me, mset[j].count,
  151. &map->magic[j], &map->nmagic[j]) == -1) {
  152. errs++;
  153. goto out;
  154. @@ -1207,7 +1213,7 @@ apprentice_load(struct magic_set *ms, const char *fn, int action)
  155. out:
  156. for (j = 0; j < MAGIC_SETS; j++)
  157. - magic_entry_free(mentry[j], mentrycount[j]);
  158. + magic_entry_free(mset[j].me, mset[j].count);
  159. if (errs) {
  160. for (j = 0; j < MAGIC_SETS; j++) {
  161. --
  162. 1.7.10.4