cherry-pick.FILE5_35-59-g8305d1cc.use-c-casts-everywhere.patch 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. Subject: Use c++ casts everywhere
  2. Origin: FILE5_35-59-g8305d1cc <https://github.com/file/file/commit/FILE5_35-59-g8305d1cc>
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Wed Feb 20 02:35:27 2019 +0000
  5. --- a/src/apprentice.c
  6. +++ b/src/apprentice.c
  7. @@ -52,10 +52,10 @@
  8. #include <limits.h>
  9. -#define EATAB {while (isascii((unsigned char) *l) && \
  10. - isspace((unsigned char) *l)) ++l;}
  11. -#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
  12. - tolower((unsigned char) (l)) : (l))
  13. +#define EATAB {while (isascii(CAST(unsigned char, *l)) && \
  14. + isspace(CAST(unsigned char, *l))) ++l;}
  15. +#define LOWCASE(l) (isupper(CAST(unsigned char, l)) ? \
  16. + tolower(CAST(unsigned char, l)) : (l))
  17. /*
  18. * Work around a bug in headers on Digital Unix.
  19. * At least confirmed for: OSF1 V4.0 878
  20. @@ -74,8 +74,8 @@
  21. #define MAP_FILE 0
  22. #endif
  23. -#define ALLOC_CHUNK (size_t)10
  24. -#define ALLOC_INCR (size_t)200
  25. +#define ALLOC_CHUNK CAST(size_t, 10)
  26. +#define ALLOC_INCR CAST(size_t, 200)
  27. #define MAP_TYPE_USER 0
  28. #define MAP_TYPE_MALLOC 1
  29. @@ -305,7 +305,7 @@
  30. {
  31. int type;
  32. - if (isalpha((unsigned char)l[1])) {
  33. + if (isalpha(CAST(unsigned char, l[1]))) {
  34. switch (l[1]) {
  35. case 'C':
  36. /* "dC" and "uC" */
  37. @@ -340,7 +340,7 @@
  38. return FILE_INVALID;
  39. }
  40. l += 2;
  41. - } else if (isdigit((unsigned char)l[1])) {
  42. + } else if (isdigit(CAST(unsigned char, l[1]))) {
  43. /*
  44. * "d{num}" and "u{num}"; we only support {num} values
  45. * of 1, 2, 4, and 8 - the Single UNIX Specification
  46. @@ -351,7 +351,7 @@
  47. * neither of them support values bigger than 8 or
  48. * non-power-of-2 values.
  49. */
  50. - if (isdigit((unsigned char)l[2])) {
  51. + if (isdigit(CAST(unsigned char, l[2]))) {
  52. /* Multi-digit, so > 9 */
  53. return FILE_INVALID;
  54. }
  55. @@ -437,8 +437,8 @@
  56. if (magicsize != FILE_MAGICSIZE) {
  57. file_error(ms, 0, "magic element size %lu != %lu",
  58. - (unsigned long)sizeof(*map->magic[0]),
  59. - (unsigned long)FILE_MAGICSIZE);
  60. + CAST(unsigned long, sizeof(*map->magic[0])),
  61. + CAST(unsigned long, FILE_MAGICSIZE));
  62. return -1;
  63. }
  64. @@ -451,7 +451,7 @@
  65. #ifndef COMPILE_ONLY
  66. map = apprentice_map(ms, fn);
  67. - if (map == (struct magic_map *)-1)
  68. + if (map == RCAST(struct magic_map *, -1))
  69. return -1;
  70. if (map == NULL) {
  71. if (ms->flags & MAGIC_CHECK)
  72. @@ -503,7 +503,7 @@
  73. struct magic_set *ms;
  74. size_t i, len;
  75. - if ((ms = CAST(struct magic_set *, calloc((size_t)1,
  76. + if ((ms = CAST(struct magic_set *, calloc(CAST(size_t, 1u),
  77. sizeof(struct magic_set)))) == NULL)
  78. return NULL;
  79. @@ -831,7 +831,7 @@
  80. case FILE_LEDOUBLE:
  81. return 8;
  82. default:
  83. - return (size_t)~0;
  84. + return CAST(size_t, ~0);
  85. }
  86. }
  87. @@ -886,7 +886,7 @@
  88. case FILE_BEDOUBLE:
  89. case FILE_LEDOUBLE:
  90. ts = typesize(m->type);
  91. - if (ts == (size_t)~0)
  92. + if (ts == CAST(size_t, ~0))
  93. abort();
  94. val += ts * MULT;
  95. break;
  96. @@ -1101,7 +1101,7 @@
  97. break;
  98. /* binary test if pattern is not text */
  99. - if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
  100. + if (file_looks_utf8(m->value.us, CAST(size_t, m->vallen), NULL,
  101. NULL) <= 0)
  102. mstart->flag |= BINTEST;
  103. else
  104. @@ -1182,7 +1182,7 @@
  105. size_t i;
  106. for (i = 0; bang[i].name != NULL; i++) {
  107. - if ((size_t)(len - 2) > bang[i].len &&
  108. + if (CAST(size_t, len - 2) > bang[i].len &&
  109. memcmp(bang[i].name, line + 2,
  110. bang[i].len) == 0)
  111. break;
  112. @@ -1235,7 +1235,7 @@
  113. private int
  114. cmpstrp(const void *p1, const void *p2)
  115. {
  116. - return strcmp(*(char *const *)p1, *(char *const *)p2);
  117. + return strcmp(*RCAST(char *const *, p1), *RCAST(char *const *, p2));
  118. }
  119. @@ -1261,10 +1261,10 @@
  120. if (me[i].mp->flag & BINTEST) {
  121. char *p = strstr(me[i].mp->desc, text);
  122. if (p && (p == me[i].mp->desc ||
  123. - isspace((unsigned char)p[-1])) &&
  124. + isspace(CAST(unsigned char, p[-1]))) &&
  125. (p + len - me[i].mp->desc == MAXstring
  126. || (p[len] == '\0' ||
  127. - isspace((unsigned char)p[len]))))
  128. + isspace(CAST(unsigned char, p[len])))))
  129. (void)fprintf(stderr, "*** Possible "
  130. "binary test for text type\n");
  131. }
  132. @@ -1460,12 +1460,12 @@
  133. * the sign extension must have happened.
  134. */
  135. case FILE_BYTE:
  136. - v = (signed char) v;
  137. + v = CAST(signed char, v);
  138. break;
  139. case FILE_SHORT:
  140. case FILE_BESHORT:
  141. case FILE_LESHORT:
  142. - v = (short) v;
  143. + v = CAST(short, v);
  144. break;
  145. case FILE_DATE:
  146. case FILE_BEDATE:
  147. @@ -1482,7 +1482,7 @@
  148. case FILE_FLOAT:
  149. case FILE_BEFLOAT:
  150. case FILE_LEFLOAT:
  151. - v = (int32_t) v;
  152. + v = CAST(int32_t, v);
  153. break;
  154. case FILE_QUAD:
  155. case FILE_BEQUAD:
  156. @@ -1499,7 +1499,7 @@
  157. case FILE_DOUBLE:
  158. case FILE_BEDOUBLE:
  159. case FILE_LEDOUBLE:
  160. - v = (int64_t) v;
  161. + v = CAST(int64_t, v);
  162. break;
  163. case FILE_STRING:
  164. case FILE_PSTRING:
  165. @@ -1626,7 +1626,7 @@
  166. for (p = cond_tbl; p->len; p++) {
  167. if (strncmp(l, p->name, p->len) == 0 &&
  168. - isspace((unsigned char)l[p->len])) {
  169. + isspace(CAST(unsigned char, l[p->len]))) {
  170. if (t)
  171. *t = l + p->len;
  172. break;
  173. @@ -1684,7 +1684,7 @@
  174. {
  175. const char *l = *lp;
  176. - while (!isspace((unsigned char)*++l))
  177. + while (!isspace(CAST(unsigned char, *++l)))
  178. switch (*l) {
  179. case CHAR_INDIRECT_RELATIVE:
  180. m->str_flags |= INDIRECT_RELATIVE;
  181. @@ -1710,7 +1710,7 @@
  182. ++l;
  183. m->mask_op |= op;
  184. - val = (uint64_t)strtoull(l, &t, 0);
  185. + val = CAST(uint64_t, strtoull(l, &t, 0));
  186. l = t;
  187. m->num_mask = file_signextend(ms, m, val);
  188. eatsize(&l);
  189. @@ -1724,7 +1724,7 @@
  190. char *t;
  191. int have_range = 0;
  192. - while (!isspace((unsigned char)*++l)) {
  193. + while (!isspace(CAST(unsigned char, *++l))) {
  194. switch (*l) {
  195. case '0': case '1': case '2':
  196. case '3': case '4': case '5':
  197. @@ -1806,7 +1806,7 @@
  198. goto out;
  199. }
  200. /* allow multiple '/' for readability */
  201. - if (l[1] == '/' && !isspace((unsigned char)l[2]))
  202. + if (l[1] == '/' && !isspace(CAST(unsigned char, l[2])))
  203. l++;
  204. }
  205. if (string_modifier_check(ms, m) == -1)
  206. @@ -1861,7 +1861,7 @@
  207. return -1;
  208. }
  209. m = &me->mp[me->cont_count - 1];
  210. - diff = (int32_t)cont_level - (int32_t)m->cont_level;
  211. + diff = CAST(int32_t, cont_level) - CAST(int32_t, m->cont_level);
  212. if (diff > 1)
  213. file_magwarn(ms, "New continuation level %u is more "
  214. "than one larger than current level %u", cont_level,
  215. @@ -1920,7 +1920,7 @@
  216. }
  217. /* get offset, then skip over it */
  218. - m->offset = (int32_t)strtol(l, &t, 0);
  219. + m->offset = CAST(int32_t, strtol(l, &t, 0));
  220. if (l == t) {
  221. if (ms->flags & MAGIC_CHECK)
  222. file_magwarn(ms, "offset `%s' invalid", l);
  223. @@ -2018,8 +2018,8 @@
  224. m->in_op |= FILE_OPINDIRECT;
  225. l++;
  226. }
  227. - if (isdigit((unsigned char)*l) || *l == '-') {
  228. - m->in_offset = (int32_t)strtol(l, &t, 0);
  229. + if (isdigit(CAST(unsigned char, *l)) || *l == '-') {
  230. + m->in_offset = CAST(int32_t, strtol(l, &t, 0));
  231. if (l == t) {
  232. if (ms->flags & MAGIC_CHECK)
  233. file_magwarn(ms,
  234. @@ -2082,7 +2082,8 @@
  235. */
  236. if (*l == 'd')
  237. m->type = get_standard_integer_type(l, &l);
  238. - else if (*l == 's' && !isalpha((unsigned char)l[1])) {
  239. + else if (*l == 's'
  240. + && !isalpha(CAST(unsigned char, l[1]))) {
  241. m->type = FILE_STRING;
  242. ++l;
  243. }
  244. @@ -2172,8 +2173,8 @@
  245. break;
  246. default:
  247. m->reln = '='; /* the default relation */
  248. - if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
  249. - isspace((unsigned char)l[1])) || !l[1])) {
  250. + if (*l == 'x' && ((isascii(CAST(unsigned char, l[1])) &&
  251. + isspace(CAST(unsigned char, l[1]))) || !l[1])) {
  252. m->reln = *l;
  253. ++l;
  254. }
  255. @@ -2270,11 +2271,11 @@
  256. file_magwarn(ms, "Too large factor `%lu'", factor);
  257. goto out;
  258. }
  259. - if (*el && !isspace((unsigned char)*el)) {
  260. + if (*el && !isspace(CAST(unsigned char, *el))) {
  261. file_magwarn(ms, "Bad factor `%s'", l);
  262. goto out;
  263. }
  264. - m->factor = (uint8_t)factor;
  265. + m->factor = CAST(uint8_t, factor);
  266. if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
  267. file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
  268. m->factor_op, m->factor);
  269. @@ -2305,7 +2306,7 @@
  270. if (buf[0] != '\0') {
  271. len = nt ? strlen(buf) : len;
  272. file_magwarn(ms, "Current entry already has a %s type "
  273. - "`%.*s', new type `%s'", name, (int)len, buf, l);
  274. + "`%.*s', new type `%s'", name, CAST(int, len), buf, l);
  275. return -1;
  276. }
  277. @@ -2326,7 +2327,7 @@
  278. file_magwarn(ms, "%s type `%s' truncated %"
  279. SIZE_T_FORMAT "u", name, line, i);
  280. } else {
  281. - if (!isspace((unsigned char)*l) && !goodchar(*l, extra))
  282. + if (!isspace(CAST(unsigned char, *l)) && !goodchar(*l, extra))
  283. file_magwarn(ms, "%s type `%s' has bad char '%c'",
  284. name, line, *l);
  285. if (nt)
  286. @@ -2428,7 +2429,7 @@
  287. if (*ptr == '#')
  288. ptr++;
  289. #define CHECKLEN() do { \
  290. - for (len = cnt = 0; isdigit((unsigned char)*ptr); ptr++, cnt++) \
  291. + for (len = cnt = 0; isdigit(CAST(unsigned char, *ptr)); ptr++, cnt++) \
  292. len = len * 10 + (*ptr - '0'); \
  293. if (cnt > 5 || len > 1024) \
  294. goto toolong; \
  295. @@ -2546,11 +2547,11 @@
  296. case FILE_FMT_STR:
  297. if (*ptr == '-')
  298. ptr++;
  299. - while (isdigit((unsigned char )*ptr))
  300. + while (isdigit(CAST(unsigned char, *ptr)))
  301. ptr++;
  302. if (*ptr == '.') {
  303. ptr++;
  304. - while (isdigit((unsigned char )*ptr))
  305. + while (isdigit(CAST(unsigned char , *ptr)))
  306. ptr++;
  307. }
  308. @@ -2695,7 +2696,7 @@
  309. return 0;
  310. default:
  311. errno = 0;
  312. - ull = (uint64_t)strtoull(*p, &ep, 0);
  313. + ull = CAST(uint64_t, strtoull(*p, &ep, 0));
  314. m->value.q = file_signextend(ms, m, ull);
  315. if (*p == ep) {
  316. file_magwarn(ms, "Unparseable number `%s'", *p);
  317. @@ -2704,24 +2705,24 @@
  318. uint64_t x;
  319. const char *q;
  320. - if (ts == (size_t)~0) {
  321. + if (ts == CAST(size_t, ~0)) {
  322. file_magwarn(ms,
  323. "Expected numeric type got `%s'",
  324. type_tbl[m->type].name);
  325. }
  326. - for (q = *p; isspace((unsigned char)*q); q++)
  327. + for (q = *p; isspace(CAST(unsigned char, *q)); q++)
  328. continue;
  329. if (*q == '-')
  330. - ull = -(int64_t)ull;
  331. + ull = -CAST(int64_t, ull);
  332. switch (ts) {
  333. case 1:
  334. - x = (uint64_t)(ull & ~0xffULL);
  335. + x = CAST(uint64_t, ull & ~0xffULL);
  336. break;
  337. case 2:
  338. - x = (uint64_t)(ull & ~0xffffULL);
  339. + x = CAST(uint64_t, ull & ~0xffffULL);
  340. break;
  341. case 4:
  342. - x = (uint64_t)(ull & ~0xffffffffULL);
  343. + x = CAST(uint64_t, ull & ~0xffffffffULL);
  344. break;
  345. case 8:
  346. x = 0;
  347. @@ -2761,7 +2762,7 @@
  348. int val;
  349. while ((c = *s++) != '\0') {
  350. - if (isspace((unsigned char) c))
  351. + if (isspace(CAST(unsigned char, c)))
  352. break;
  353. if (p >= pmax) {
  354. file_error(ms, 0, "string too long: `%s'", origs);
  355. @@ -2785,7 +2786,7 @@
  356. /*FALLTHROUGH*/
  357. default:
  358. if (warn) {
  359. - if (isprint((unsigned char)c)) {
  360. + if (isprint(CAST(unsigned char, c))) {
  361. /* Allow escaping of
  362. * ``relations'' */
  363. if (strchr("<>&^=!", c) == NULL
  364. @@ -2823,7 +2824,7 @@
  365. case '!':
  366. /* and baskslash itself */
  367. case '\\':
  368. - *p++ = (char) c;
  369. + *p++ = CAST(char, c);
  370. break;
  371. case 'a':
  372. @@ -2875,7 +2876,7 @@
  373. }
  374. else
  375. --s;
  376. - *p++ = (char)val;
  377. + *p++ = CAST(char, val);
  378. break;
  379. /* \x and up to 2 hex digits */
  380. @@ -2891,18 +2892,18 @@
  381. --s;
  382. } else
  383. --s;
  384. - *p++ = (char)val;
  385. + *p++ = CAST(char, val);
  386. break;
  387. }
  388. } else
  389. - *p++ = (char)c;
  390. + *p++ = CAST(char, c);
  391. }
  392. --s;
  393. out:
  394. *p = '\0';
  395. m->vallen = CAST(unsigned char, (p - origp));
  396. if (m->type == FILE_PSTRING)
  397. - m->vallen += (unsigned char)file_pstring_length_size(m);
  398. + m->vallen += CAST(unsigned char, file_pstring_length_size(m));
  399. return s;
  400. }
  401. @@ -2911,9 +2912,9 @@
  402. private int
  403. hextoint(int c)
  404. {
  405. - if (!isascii((unsigned char) c))
  406. + if (!isascii(CAST(unsigned char, c)))
  407. return -1;
  408. - if (isdigit((unsigned char) c))
  409. + if (isdigit(CAST(unsigned char, c)))
  410. return c - '0';
  411. if ((c >= 'a') && (c <= 'f'))
  412. return c + 10 - 'a';
  413. @@ -3068,11 +3069,11 @@
  414. goto error;
  415. }
  416. - map->len = (size_t)st.st_size;
  417. + map->len = CAST(size_t, st.st_size);
  418. #ifdef QUICK
  419. map->type = MAP_TYPE_MMAP;
  420. - if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
  421. - MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
  422. + if ((map->p = mmap(0, CAST(size_t, st.st_size), PROT_READ|PROT_WRITE,
  423. + MAP_PRIVATE|MAP_FILE, fd, CAST(off_t, 0))) == MAP_FAILED) {
  424. file_error(ms, errno, "cannot map `%s'", dbname);
  425. goto error;
  426. }
  427. @@ -3092,11 +3093,11 @@
  428. fd = -1;
  429. if (check_buffer(ms, map, dbname) != 0) {
  430. - rv = (struct magic_map *)-1;
  431. + rv = RCAST(struct magic_map *, -1);
  432. goto error;
  433. }
  434. #ifdef QUICK
  435. - if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) {
  436. + if (mprotect(map->p, CAST(size_t, st.st_size), PROT_READ) == -1) {
  437. file_error(ms, errno, "cannot mprotect `%s'", dbname);
  438. goto error;
  439. }
  440. @@ -3140,7 +3141,7 @@
  441. VERSIONNO, dbname, version);
  442. return -1;
  443. }
  444. - entries = (uint32_t)(map->len / sizeof(struct magic));
  445. + entries = CAST(uint32_t, map->len / sizeof(struct magic));
  446. if ((entries * sizeof(struct magic)) != map->len) {
  447. file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
  448. "a multiple of %" SIZE_T_FORMAT "u",
  449. @@ -3202,14 +3203,14 @@
  450. hdr.h[1] = VERSIONNO;
  451. memcpy(hdr.h + 2, map->nmagic, nm);
  452. - if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
  453. + if (write(fd, &hdr, sizeof(hdr)) != CAST(ssize_t, sizeof(hdr))) {
  454. file_error(ms, errno, "error writing `%s'", dbname);
  455. goto out2;
  456. }
  457. for (i = 0; i < MAGIC_SETS; i++) {
  458. len = m * map->nmagic[i];
  459. - if (write(fd, map->magic[i], len) != (ssize_t)len) {
  460. + if (write(fd, map->magic[i], len) != CAST(ssize_t, len)) {
  461. file_error(ms, errno, "error writing `%s'", dbname);
  462. goto out2;
  463. }
  464. @@ -3255,7 +3256,8 @@
  465. q++;
  466. /* Compatibility with old code that looked in .mime */
  467. if (ms->flags & MAGIC_MIME) {
  468. - if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
  469. + if (asprintf(&buf, "%.*s.mime%s", CAST(int, q - fn), fn, ext)
  470. + < 0)
  471. return NULL;
  472. if (access(buf, R_OK) != -1) {
  473. ms->flags &= MAGIC_MIME_TYPE;
  474. @@ -3263,7 +3265,7 @@
  475. }
  476. free(buf);
  477. }
  478. - if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
  479. + if (asprintf(&buf, "%.*s%s", CAST(int, q - fn), fn, ext) < 0)
  480. return NULL;
  481. /* Compatibility with old code that looked in .mime */
  482. @@ -3290,8 +3292,8 @@
  483. swap2(uint16_t sv)
  484. {
  485. uint16_t rv;
  486. - uint8_t *s = (uint8_t *)(void *)&sv;
  487. - uint8_t *d = (uint8_t *)(void *)&rv;
  488. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  489. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  490. d[0] = s[1];
  491. d[1] = s[0];
  492. return rv;
  493. @@ -3304,8 +3306,8 @@
  494. swap4(uint32_t sv)
  495. {
  496. uint32_t rv;
  497. - uint8_t *s = (uint8_t *)(void *)&sv;
  498. - uint8_t *d = (uint8_t *)(void *)&rv;
  499. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  500. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  501. d[0] = s[3];
  502. d[1] = s[2];
  503. d[2] = s[1];
  504. @@ -3320,8 +3322,8 @@
  505. swap8(uint64_t sv)
  506. {
  507. uint64_t rv;
  508. - uint8_t *s = (uint8_t *)(void *)&sv;
  509. - uint8_t *d = (uint8_t *)(void *)&rv;
  510. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  511. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  512. #if 0
  513. d[0] = s[3];
  514. d[1] = s[2];
  515. @@ -3351,9 +3353,9 @@
  516. bs1(struct magic *m)
  517. {
  518. m->cont_level = swap2(m->cont_level);
  519. - m->offset = swap4((uint32_t)m->offset);
  520. - m->in_offset = swap4((uint32_t)m->in_offset);
  521. - m->lineno = swap4((uint32_t)m->lineno);
  522. + m->offset = swap4(CAST(uint32_t, m->offset));
  523. + m->in_offset = swap4(CAST(uint32_t, m->in_offset));
  524. + m->lineno = swap4(CAST(uint32_t, m->lineno));
  525. if (IS_STRING(m->type)) {
  526. m->str_range = swap4(m->str_range);
  527. m->str_flags = swap4(m->str_flags);
  528. @@ -3385,7 +3387,7 @@
  529. file_pstring_get_length(const struct magic *m, const char *ss)
  530. {
  531. size_t len = 0;
  532. - const unsigned char *s = (const unsigned char *)ss;
  533. + const unsigned char *s = RCAST(const unsigned char *, ss);
  534. unsigned int s3, s2, s1, s0;
  535. switch (m->str_flags & PSTRING_LEN) {
  536. --- a/src/ascmagic.c
  537. +++ b/src/ascmagic.c
  538. @@ -127,7 +127,7 @@
  539. int n_nel = 0;
  540. int executable = 0;
  541. - size_t last_line_end = (size_t)-1;
  542. + size_t last_line_end = CAST(size_t, -1);
  543. int has_long_lines = 0;
  544. nbytes = trim_nuls(buf, nbytes);
  545. @@ -151,7 +151,7 @@
  546. == NULL)
  547. goto done;
  548. buffer_init(&bb, b->fd, utf8_buf,
  549. - (size_t)(utf8_end - utf8_buf));
  550. + CAST(size_t, utf8_end - utf8_buf));
  551. if ((rv = file_softmagic(ms, &bb, NULL, NULL,
  552. TEXTTEST, text)) == 0)
  553. @@ -330,42 +330,42 @@
  554. if (ubuf[i] <= 0x7f) {
  555. if (end - buf < 1)
  556. return NULL;
  557. - *buf++ = (unsigned char)ubuf[i];
  558. + *buf++ = CAST(unsigned char, ubuf[i]);
  559. } else if (ubuf[i] <= 0x7ff) {
  560. if (end - buf < 2)
  561. return NULL;
  562. - *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0);
  563. - *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  564. + *buf++ = CAST(unsigned char, (ubuf[i] >> 6) + 0xc0);
  565. + *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  566. } else if (ubuf[i] <= 0xffff) {
  567. if (end - buf < 3)
  568. return NULL;
  569. - *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0);
  570. - *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  571. - *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  572. + *buf++ = CAST(unsigned char, (ubuf[i] >> 12) + 0xe0);
  573. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  574. + *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  575. } else if (ubuf[i] <= 0x1fffff) {
  576. if (end - buf < 4)
  577. return NULL;
  578. - *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0);
  579. - *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  580. - *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  581. - *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  582. + *buf++ = CAST(unsigned char, (ubuf[i] >> 18) + 0xf0);
  583. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  584. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  585. + *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  586. } else if (ubuf[i] <= 0x3ffffff) {
  587. if (end - buf < 5)
  588. return NULL;
  589. - *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8);
  590. - *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  591. - *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  592. - *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  593. - *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  594. + *buf++ = CAST(unsigned char, (ubuf[i] >> 24) + 0xf8);
  595. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  596. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  597. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  598. + *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  599. } else if (ubuf[i] <= 0x7fffffff) {
  600. if (end - buf < 6)
  601. return NULL;
  602. - *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc);
  603. - *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80);
  604. - *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80);
  605. - *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80);
  606. - *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80);
  607. - *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80);
  608. + *buf++ = CAST(unsigned char, (ubuf[i] >> 30) + 0xfc);
  609. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 24) & 0x3f) + 0x80);
  610. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 18) & 0x3f) + 0x80);
  611. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 12) & 0x3f) + 0x80);
  612. + *buf++ = CAST(unsigned char, ((ubuf[i] >> 6) & 0x3f) + 0x80);
  613. + *buf++ = CAST(unsigned char, (ubuf[i] & 0x3f) + 0x80);
  614. } else /* Invalid character */
  615. return NULL;
  616. }
  617. --- a/src/buffer.c
  618. +++ b/src/buffer.c
  619. @@ -61,13 +61,13 @@
  620. struct buffer *b = CCAST(struct buffer *, bb);
  621. if (b->elen != 0)
  622. - return b->elen == (size_t)~0 ? -1 : 0;
  623. + return b->elen == CAST(size_t, ~0) ? -1 : 0;
  624. if (!S_ISREG(b->st.st_mode))
  625. goto out;
  626. - b->elen = (size_t)b->st.st_size < b->flen ?
  627. - (size_t)b->st.st_size : b->flen;
  628. + b->elen = CAST(size_t, b->st.st_size) < b->flen ?
  629. + CAST(size_t, b->st.st_size) : b->flen;
  630. if ((b->ebuf = malloc(b->elen)) == NULL)
  631. goto out;
  632. @@ -79,6 +79,6 @@
  633. return 0;
  634. out:
  635. - b->elen = (size_t)~0;
  636. + b->elen = CAST(size_t, ~0);
  637. return -1;
  638. }
  639. --- a/src/cdf.c
  640. +++ b/src/cdf.c
  641. @@ -35,7 +35,7 @@
  642. #include "file.h"
  643. #ifndef lint
  644. -FILE_RCSID("@(#)$File: cdf.c,v 1.113 2018/10/15 16:29:16 christos Exp $")
  645. +FILE_RCSID("@(#)$File: cdf.c,v 1.114 2019/02/20 02:35:27 christos Exp $")
  646. #endif
  647. #include <assert.h>
  648. @@ -66,11 +66,14 @@
  649. uint32_t u;
  650. } cdf_bo;
  651. -#define NEED_SWAP (cdf_bo.u == (uint32_t)0x01020304)
  652. +#define NEED_SWAP (cdf_bo.u == CAST(uint32_t, 0x01020304))
  653. -#define CDF_TOLE8(x) ((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x)))
  654. -#define CDF_TOLE4(x) ((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x)))
  655. -#define CDF_TOLE2(x) ((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x)))
  656. +#define CDF_TOLE8(x) \
  657. + (CAST(uint64_t, NEED_SWAP ? _cdf_tole8(x) : CAST(uint64_t, x)))
  658. +#define CDF_TOLE4(x) \
  659. + (CAST(uint32_t, NEED_SWAP ? _cdf_tole4(x) : CAST(uint32_t, x)))
  660. +#define CDF_TOLE2(x) \
  661. + (CAST(uint16_t, NEED_SWAP ? _cdf_tole2(x) : CAST(uint16_t, x)))
  662. #define CDF_TOLE(x) (/*CONSTCOND*/sizeof(x) == 2 ? \
  663. CDF_TOLE2(CAST(uint16_t, x)) : \
  664. (/*CONSTCOND*/sizeof(x) == 4 ? \
  665. @@ -120,8 +123,8 @@
  666. _cdf_tole2(uint16_t sv)
  667. {
  668. uint16_t rv;
  669. - uint8_t *s = (uint8_t *)(void *)&sv;
  670. - uint8_t *d = (uint8_t *)(void *)&rv;
  671. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  672. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  673. d[0] = s[1];
  674. d[1] = s[0];
  675. return rv;
  676. @@ -134,8 +137,8 @@
  677. _cdf_tole4(uint32_t sv)
  678. {
  679. uint32_t rv;
  680. - uint8_t *s = (uint8_t *)(void *)&sv;
  681. - uint8_t *d = (uint8_t *)(void *)&rv;
  682. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  683. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  684. d[0] = s[3];
  685. d[1] = s[2];
  686. d[2] = s[1];
  687. @@ -150,8 +153,8 @@
  688. _cdf_tole8(uint64_t sv)
  689. {
  690. uint64_t rv;
  691. - uint8_t *s = (uint8_t *)(void *)&sv;
  692. - uint8_t *d = (uint8_t *)(void *)&rv;
  693. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  694. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  695. d[0] = s[7];
  696. d[1] = s[6];
  697. d[2] = s[5];
  698. @@ -216,15 +219,17 @@
  699. h->h_min_size_standard_stream =
  700. CDF_TOLE4(h->h_min_size_standard_stream);
  701. h->h_secid_first_sector_in_short_sat =
  702. - CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_short_sat);
  703. + CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_short_sat));
  704. h->h_num_sectors_in_short_sat =
  705. CDF_TOLE4(h->h_num_sectors_in_short_sat);
  706. h->h_secid_first_sector_in_master_sat =
  707. - CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_master_sat);
  708. + CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_master_sat));
  709. h->h_num_sectors_in_master_sat =
  710. CDF_TOLE4(h->h_num_sectors_in_master_sat);
  711. - for (i = 0; i < __arraycount(h->h_master_sat); i++)
  712. - h->h_master_sat[i] = CDF_TOLE4((uint32_t)h->h_master_sat[i]);
  713. + for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  714. + h->h_master_sat[i] =
  715. + CDF_TOLE4(CAST(uint32_t, h->h_master_sat[i]));
  716. + }
  717. }
  718. void
  719. @@ -257,15 +262,16 @@
  720. cdf_swap_dir(cdf_directory_t *d)
  721. {
  722. d->d_namelen = CDF_TOLE2(d->d_namelen);
  723. - d->d_left_child = CDF_TOLE4((uint32_t)d->d_left_child);
  724. - d->d_right_child = CDF_TOLE4((uint32_t)d->d_right_child);
  725. - d->d_storage = CDF_TOLE4((uint32_t)d->d_storage);
  726. + d->d_left_child = CDF_TOLE4(CAST(uint32_t, d->d_left_child));
  727. + d->d_right_child = CDF_TOLE4(CAST(uint32_t, d->d_right_child));
  728. + d->d_storage = CDF_TOLE4(CAST(uint32_t, d->d_storage));
  729. d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
  730. d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
  731. d->d_flags = CDF_TOLE4(d->d_flags);
  732. - d->d_created = CDF_TOLE8((uint64_t)d->d_created);
  733. - d->d_modified = CDF_TOLE8((uint64_t)d->d_modified);
  734. - d->d_stream_first_sector = CDF_TOLE4((uint32_t)d->d_stream_first_sector);
  735. + d->d_created = CDF_TOLE8(CAST(uint64_t, d->d_created));
  736. + d->d_modified = CDF_TOLE8(CAST(uint64_t, d->d_modified));
  737. + d->d_stream_first_sector = CDF_TOLE4(
  738. + CAST(uint32_t, d->d_stream_first_sector));
  739. d->d_size = CDF_TOLE4(d->d_size);
  740. }
  741. @@ -322,11 +328,11 @@
  742. cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
  743. const void *p, size_t tail, int line)
  744. {
  745. - const char *b = (const char *)sst->sst_tab;
  746. - const char *e = ((const char *)p) + tail;
  747. + const char *b = RCAST(const char *, sst->sst_tab);
  748. + const char *e = RCAST(const char *, p) + tail;
  749. size_t ss = cdf_check_stream(sst, h);
  750. /*LINTED*/(void)&line;
  751. - if (e >= b && (size_t)(e - b) <= ss * sst->sst_len)
  752. + if (e >= b && CAST(size_t, e - b) <= ss * sst->sst_len)
  753. return 0;
  754. DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u"
  755. " > %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
  756. @@ -339,23 +345,23 @@
  757. static ssize_t
  758. cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
  759. {
  760. - size_t siz = (size_t)off + len;
  761. + size_t siz = CAST(size_t, off + len);
  762. - if ((off_t)(off + len) != (off_t)siz)
  763. + if (CAST(off_t, off + len) != CAST(off_t, siz))
  764. goto out;
  765. if (info->i_buf != NULL && info->i_len >= siz) {
  766. (void)memcpy(buf, &info->i_buf[off], len);
  767. - return (ssize_t)len;
  768. + return CAST(ssize_t, len);
  769. }
  770. if (info->i_fd == -1)
  771. goto out;
  772. - if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
  773. + if (pread(info->i_fd, buf, len, off) != CAST(ssize_t, len))
  774. return -1;
  775. - return (ssize_t)len;
  776. + return CAST(ssize_t, len);
  777. out:
  778. errno = EINVAL;
  779. return -1;
  780. @@ -367,7 +373,7 @@
  781. char buf[512];
  782. (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
  783. - if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
  784. + if (cdf_read(info, CAST(off_t, 0), buf, sizeof(buf)) == -1)
  785. return -1;
  786. cdf_unpack_header(h, buf);
  787. cdf_swap_header(h);
  788. @@ -401,7 +407,7 @@
  789. size_t ss = CDF_SEC_SIZE(h);
  790. size_t pos = CDF_SEC_POS(h, id);
  791. assert(ss == len);
  792. - return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len);
  793. + return cdf_read(info, CAST(off_t, pos), RCAST(char *, buf) + offs, len);
  794. }
  795. ssize_t
  796. @@ -417,8 +423,8 @@
  797. pos + len, CDF_SEC_SIZE(h) * sst->sst_len));
  798. goto out;
  799. }
  800. - (void)memcpy(((char *)buf) + offs,
  801. - ((const char *)sst->sst_tab) + pos, len);
  802. + (void)memcpy(RCAST(char *, buf) + offs,
  803. + RCAST(const char *, sst->sst_tab) + pos, len);
  804. return len;
  805. out:
  806. errno = EFTYPE;
  807. @@ -461,7 +467,7 @@
  808. if (h->h_master_sat[i] < 0)
  809. break;
  810. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  811. - h->h_master_sat[i]) != (ssize_t)ss) {
  812. + h->h_master_sat[i]) != CAST(ssize_t, ss)) {
  813. DPRINTF(("Reading sector %d", h->h_master_sat[i]));
  814. goto out1;
  815. }
  816. @@ -478,12 +484,13 @@
  817. DPRINTF(("Reading master sector loop limit"));
  818. goto out3;
  819. }
  820. - if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
  821. + if (cdf_read_sector(info, msa, 0, ss, h, mid) !=
  822. + CAST(ssize_t, ss)) {
  823. DPRINTF(("Reading master sector %d", mid));
  824. goto out2;
  825. }
  826. for (k = 0; k < nsatpersec; k++, i++) {
  827. - sec = CDF_TOLE4((uint32_t)msa[k]);
  828. + sec = CDF_TOLE4(CAST(uint32_t, msa[k]));
  829. if (sec < 0)
  830. goto out;
  831. if (i >= sat->sat_len) {
  832. @@ -493,13 +500,13 @@
  833. goto out3;
  834. }
  835. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  836. - sec) != (ssize_t)ss) {
  837. + sec) != CAST(ssize_t, ss)) {
  838. DPRINTF(("Reading sector %d",
  839. CDF_TOLE4(msa[k])));
  840. goto out2;
  841. }
  842. }
  843. - mid = CDF_TOLE4((uint32_t)msa[nsatpersec]);
  844. + mid = CDF_TOLE4(CAST(uint32_t, msa[nsatpersec]));
  845. }
  846. out:
  847. sat->sat_len = i;
  848. @@ -518,7 +525,7 @@
  849. cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
  850. {
  851. size_t i, j;
  852. - cdf_secid_t maxsector = (cdf_secid_t)((sat->sat_len * size)
  853. + cdf_secid_t maxsector = CAST(cdf_secid_t, (sat->sat_len * size)
  854. / sizeof(maxsector));
  855. DPRINTF(("Chain:"));
  856. @@ -538,7 +545,7 @@
  857. DPRINTF(("Sector %d >= %d\n", sid, maxsector));
  858. goto out;
  859. }
  860. - sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
  861. + sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
  862. }
  863. if (i == 0) {
  864. DPRINTF((" none, sid: %d\n", sid));
  865. @@ -549,7 +556,7 @@
  866. return i;
  867. out:
  868. errno = EFTYPE;
  869. - return (size_t)-1;
  870. + return CAST(size_t, -1);
  871. }
  872. int
  873. @@ -566,7 +573,7 @@
  874. if (sid == CDF_SECID_END_OF_CHAIN || len == 0)
  875. return cdf_zero_stream(scn);
  876. - if (scn->sst_len == (size_t)-1)
  877. + if (scn->sst_len == CAST(size_t, -1))
  878. goto out;
  879. scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
  880. @@ -585,7 +592,7 @@
  881. goto out;
  882. }
  883. if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
  884. - sid)) != (ssize_t)ss) {
  885. + sid)) != CAST(ssize_t, ss)) {
  886. if (i == scn->sst_len - 1 && nr > 0) {
  887. /* Last sector might be truncated */
  888. return 0;
  889. @@ -593,7 +600,7 @@
  890. DPRINTF(("Reading long sector chain %d", sid));
  891. goto out;
  892. }
  893. - sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
  894. + sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
  895. }
  896. return 0;
  897. out:
  898. @@ -612,7 +619,7 @@
  899. scn->sst_dirlen = len;
  900. scn->sst_ss = ss;
  901. - if (scn->sst_len == (size_t)-1)
  902. + if (scn->sst_len == CAST(size_t, -1))
  903. goto out;
  904. scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
  905. @@ -631,11 +638,11 @@
  906. goto out;
  907. }
  908. if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
  909. - sid) != (ssize_t)ss) {
  910. + sid) != CAST(ssize_t, ss)) {
  911. DPRINTF(("Reading short sector chain %d", sid));
  912. goto out;
  913. }
  914. - sid = CDF_TOLE4((uint32_t)ssat->sat_tab[sid]);
  915. + sid = CDF_TOLE4(CAST(uint32_t, ssat->sat_tab[sid]));
  916. }
  917. return 0;
  918. out:
  919. @@ -666,7 +673,7 @@
  920. cdf_secid_t sid = h->h_secid_first_directory;
  921. ns = cdf_count_chain(sat, sid, ss);
  922. - if (ns == (size_t)-1)
  923. + if (ns == CAST(size_t, -1))
  924. return -1;
  925. nd = ss / CDF_DIRECTORY_SIZE;
  926. @@ -687,7 +694,8 @@
  927. DPRINTF(("Read dir loop limit"));
  928. goto out;
  929. }
  930. - if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
  931. + if (cdf_read_sector(info, buf, 0, ss, h, sid) !=
  932. + CAST(ssize_t, ss)) {
  933. DPRINTF(("Reading directory sector %d", sid));
  934. goto out;
  935. }
  936. @@ -695,7 +703,7 @@
  937. cdf_unpack_dir(&dir->dir_tab[i * nd + j],
  938. &buf[j * CDF_DIRECTORY_SIZE]);
  939. }
  940. - sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
  941. + sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
  942. }
  943. if (NEED_SWAP)
  944. for (i = 0; i < dir->dir_len; i++)
  945. @@ -720,7 +728,7 @@
  946. ssat->sat_tab = NULL;
  947. ssat->sat_len = cdf_count_chain(sat, sid, ss);
  948. - if (ssat->sat_len == (size_t)-1)
  949. + if (ssat->sat_len == CAST(size_t, -1))
  950. goto out;
  951. ssat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(ssat->sat_len, ss));
  952. @@ -739,11 +747,11 @@
  953. goto out;
  954. }
  955. if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
  956. - (ssize_t)ss) {
  957. + CAST(ssize_t, ss)) {
  958. DPRINTF(("Reading short sat sector %d", sid));
  959. goto out1;
  960. }
  961. - sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
  962. + sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
  963. }
  964. return 0;
  965. out:
  966. @@ -793,7 +801,7 @@
  967. {
  968. for (; l--; d++, s++)
  969. if (*d != CDF_TOLE2(*s))
  970. - return (unsigned char)*d - CDF_TOLE2(*s);
  971. + return CAST(unsigned char, *d) - CDF_TOLE2(*s);
  972. return 0;
  973. }
  974. @@ -924,7 +932,7 @@
  975. if (inp->pi_type & CDF_VECTOR)
  976. return 0;
  977. - if ((size_t)(CAST(const char *, e) - CAST(const char *, p)) < len)
  978. + if (CAST(size_t, CAST(const char *, e) - CAST(const char *, p)) < len)
  979. return 0;
  980. (void)memcpy(&inp->pi_val, p, len);
  981. @@ -1110,8 +1118,9 @@
  982. const cdf_summary_info_header_t *si =
  983. CAST(const cdf_summary_info_header_t *, sst->sst_tab);
  984. const cdf_section_declaration_t *sd =
  985. - CAST(const cdf_section_declaration_t *, (const void *)
  986. - ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));
  987. + CAST(const cdf_section_declaration_t *, RCAST(const void *,
  988. + RCAST(const char *, sst->sst_tab)
  989. + + CDF_SECTION_DECLARATION_OFFSET));
  990. if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
  991. cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
  992. @@ -1262,28 +1271,28 @@
  993. int days, hours, mins, secs;
  994. ts /= CDF_TIME_PREC;
  995. - secs = (int)(ts % 60);
  996. + secs = CAST(int, ts % 60);
  997. ts /= 60;
  998. - mins = (int)(ts % 60);
  999. + mins = CAST(int, ts % 60);
  1000. ts /= 60;
  1001. - hours = (int)(ts % 24);
  1002. + hours = CAST(int, ts % 24);
  1003. ts /= 24;
  1004. - days = (int)ts;
  1005. + days = CAST(int, ts);
  1006. if (days) {
  1007. len += snprintf(buf + len, bufsiz - len, "%dd+", days);
  1008. - if ((size_t)len >= bufsiz)
  1009. + if (CAST(size_t, len) >= bufsiz)
  1010. return len;
  1011. }
  1012. if (days || hours) {
  1013. len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
  1014. - if ((size_t)len >= bufsiz)
  1015. + if (CAST(size_t, len) >= bufsiz)
  1016. return len;
  1017. }
  1018. len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
  1019. - if ((size_t)len >= bufsiz)
  1020. + if (CAST(size_t, len) >= bufsiz)
  1021. return len;
  1022. len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
  1023. @@ -1295,7 +1304,7 @@
  1024. {
  1025. size_t i;
  1026. for (i = 0; i < len && p[i]; i++)
  1027. - buf[i] = (char)p[i];
  1028. + buf[i] = CAST(char, p[i]);
  1029. buf[i] = '\0';
  1030. return buf;
  1031. }
  1032. --- a/src/cdf.h
  1033. +++ b/src/cdf.h
  1034. @@ -76,9 +76,9 @@
  1035. cdf_secid_t h_master_sat[436/4];
  1036. } cdf_header_t;
  1037. -#define CDF_SEC_SIZE(h) ((size_t)(1 << (h)->h_sec_size_p2))
  1038. +#define CDF_SEC_SIZE(h) CAST(size_t, 1 << (h)->h_sec_size_p2)
  1039. #define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
  1040. -#define CDF_SHORT_SEC_SIZE(h) ((size_t)(1 << (h)->h_short_sec_size_p2))
  1041. +#define CDF_SHORT_SEC_SIZE(h) CAST(size_t, 1 << (h)->h_short_sec_size_p2)
  1042. #define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
  1043. typedef int32_t cdf_dirid_t;
  1044. --- a/src/cdf_time.c
  1045. +++ b/src/cdf_time.c
  1046. @@ -27,7 +27,7 @@
  1047. #include "file.h"
  1048. #ifndef lint
  1049. -FILE_RCSID("@(#)$File: cdf_time.c,v 1.17 2018/09/09 20:33:28 christos Exp $")
  1050. +FILE_RCSID("@(#)$File: cdf_time.c,v 1.18 2019/02/20 02:35:27 christos Exp $")
  1051. #endif
  1052. #include <time.h>
  1053. @@ -90,9 +90,9 @@
  1054. if (m == 1 && isleap(year))
  1055. days--;
  1056. if (days <= 0)
  1057. - return (int)m;
  1058. + return CAST(int, m);
  1059. }
  1060. - return (int)m;
  1061. + return CAST(int, m);
  1062. }
  1063. int
  1064. @@ -108,22 +108,22 @@
  1065. ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
  1066. t /= CDF_TIME_PREC;
  1067. - tm.tm_sec = (int)(t % 60);
  1068. + tm.tm_sec = CAST(int, t % 60);
  1069. t /= 60;
  1070. - tm.tm_min = (int)(t % 60);
  1071. + tm.tm_min = CAST(int, t % 60);
  1072. t /= 60;
  1073. - tm.tm_hour = (int)(t % 24);
  1074. + tm.tm_hour = CAST(int, t % 24);
  1075. t /= 24;
  1076. /* XXX: Approx */
  1077. - tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
  1078. + tm.tm_year = CAST(int, CDF_BASE_YEAR + (t / 365));
  1079. rdays = cdf_getdays(tm.tm_year);
  1080. t -= rdays - 1;
  1081. - tm.tm_mday = cdf_getday(tm.tm_year, (int)t);
  1082. - tm.tm_mon = cdf_getmonth(tm.tm_year, (int)t);
  1083. + tm.tm_mday = cdf_getday(tm.tm_year, CAST(int, t));
  1084. + tm.tm_mon = cdf_getmonth(tm.tm_year, CAST(int, t));
  1085. tm.tm_wday = 0;
  1086. tm.tm_yday = 0;
  1087. tm.tm_isdst = 0;
  1088. @@ -172,7 +172,7 @@
  1089. if (ptr != NULL)
  1090. return buf;
  1091. (void)snprintf(buf, 26, "*Bad* %#16.16" INT64_T_FORMAT "x\n",
  1092. - (long long)*sec);
  1093. + CAST(long long, *sec));
  1094. return buf;
  1095. }
  1096. --- a/src/compress.c
  1097. +++ b/src/compress.c
  1098. @@ -378,7 +378,7 @@
  1099. (void)ioctl(fd, FIONREAD, &t);
  1100. }
  1101. - if (t > 0 && (size_t)t < n) {
  1102. + if (t > 0 && CAST(size_t, t) < n) {
  1103. n = t;
  1104. rn = n;
  1105. }
  1106. @@ -436,11 +436,11 @@
  1107. return -1;
  1108. }
  1109. - if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  1110. + if (swrite(tfd, startbuf, nbytes) != CAST(ssize_t, nbytes))
  1111. r = 1;
  1112. else {
  1113. while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
  1114. - if (swrite(tfd, buf, (size_t)r) != r)
  1115. + if (swrite(tfd, buf, CAST(size_t, r)) != r)
  1116. break;
  1117. }
  1118. @@ -465,7 +465,7 @@
  1119. return -1;
  1120. }
  1121. (void)close(tfd);
  1122. - if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  1123. + if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1)) {
  1124. file_badseek(ms);
  1125. return -1;
  1126. }
  1127. @@ -542,7 +542,7 @@
  1128. if (rc != Z_OK && rc != Z_STREAM_END)
  1129. goto err;
  1130. - *n = (size_t)z.total_out;
  1131. + *n = CAST(size_t, z.total_out);
  1132. rc = inflateEnd(&z);
  1133. if (rc != Z_OK)
  1134. goto err;
  1135. @@ -552,8 +552,8 @@
  1136. return OKDATA;
  1137. err:
  1138. - strlcpy((char *)*newch, z.msg ? z.msg : zError(rc), bytes_max);
  1139. - *n = strlen((char *)*newch);
  1140. + strlcpy(RCAST(char *, *newch), z.msg ? z.msg : zError(rc), bytes_max);
  1141. + *n = strlen(RCAST(char *, *newch));
  1142. return ERRDATA;
  1143. }
  1144. #endif
  1145. @@ -573,7 +573,7 @@
  1146. *len = 0;
  1147. return NODATA;
  1148. }
  1149. - *buf = (unsigned char *)msg;
  1150. + *buf = RCAST(unsigned char *, msg);
  1151. *len = strlen(msg);
  1152. return ERRDATA;
  1153. }
  1154. @@ -621,7 +621,7 @@
  1155. switch (fork()) {
  1156. case 0: /* child */
  1157. closefd(fdp[STDOUT_FILENO], 0);
  1158. - if (swrite(fdp[STDIN_FILENO][1], old, n) != (ssize_t)n) {
  1159. + if (swrite(fdp[STDIN_FILENO][1], old, n) != CAST(ssize_t, n)) {
  1160. DPRINTF("Write failed (%s)\n", strerror(errno));
  1161. exit(1);
  1162. }
  1163. @@ -650,17 +650,17 @@
  1164. char *buf;
  1165. ubuf[n] = '\0';
  1166. - buf = (char *)ubuf;
  1167. - while (isspace((unsigned char)*buf))
  1168. + buf = RCAST(char *, ubuf);
  1169. + while (isspace(CAST(unsigned char, *buf)))
  1170. buf++;
  1171. DPRINTF("Filter error[[[%s]]]\n", buf);
  1172. - if ((p = strchr((char *)buf, '\n')) != NULL)
  1173. + if ((p = strchr(CAST(char *, buf), '\n')) != NULL)
  1174. *p = '\0';
  1175. - if ((p = strchr((char *)buf, ';')) != NULL)
  1176. + if ((p = strchr(CAST(char *, buf), ';')) != NULL)
  1177. *p = '\0';
  1178. - if ((p = strrchr((char *)buf, ':')) != NULL) {
  1179. + if ((p = strrchr(CAST(char *, buf), ':')) != NULL) {
  1180. ++p;
  1181. - while (isspace((unsigned char)*p))
  1182. + while (isspace(CAST(unsigned char, *p)))
  1183. p++;
  1184. n = strlen(p);
  1185. memmove(ubuf, p, CAST(size_t, n + 1));
  1186. @@ -715,14 +715,14 @@
  1187. case 0: /* child */
  1188. if (fd != -1) {
  1189. fdp[STDIN_FILENO][0] = fd;
  1190. - (void) lseek(fd, (off_t)0, SEEK_SET);
  1191. + (void) lseek(fd, CAST(off_t, 0), SEEK_SET);
  1192. }
  1193. for (i = 0; i < __arraycount(fdp); i++)
  1194. copydesc(CAST(int, i), fdp[i]);
  1195. (void)execvp(compr[method].argv[0],
  1196. - (char *const *)(intptr_t)compr[method].argv);
  1197. + RCAST(char *const *, RCAST(intptr_t, compr[method].argv)));
  1198. dprintf(STDERR_FILENO, "exec `%s' failed, %s",
  1199. compr[method].argv[0], strerror(errno));
  1200. exit(1);
  1201. --- a/src/der.c
  1202. +++ b/src/der.c
  1203. @@ -35,7 +35,7 @@
  1204. #include "file.h"
  1205. #ifndef lint
  1206. -FILE_RCSID("@(#)$File: der.c,v 1.15 2018/10/15 16:29:16 christos Exp $")
  1207. +FILE_RCSID("@(#)$File: der.c,v 1.16 2019/02/20 02:35:27 christos Exp $")
  1208. #endif
  1209. #endif
  1210. @@ -56,7 +56,7 @@
  1211. #include <err.h>
  1212. #endif
  1213. -#define DER_BAD ((uint32_t)-1)
  1214. +#define DER_BAD CAST(uint32_t, -1)
  1215. #define DER_CLASS_UNIVERSAL 0
  1216. #define DER_CLASS_APPLICATION 1
  1217. @@ -224,7 +224,7 @@
  1218. case DER_TAG_UTF8_STRING:
  1219. case DER_TAG_IA5_STRING:
  1220. case DER_TAG_UTCTIME:
  1221. - return snprintf(buf, blen, "%.*s", len, (const char *)q);
  1222. + return snprintf(buf, blen, "%.*s", len, RCAST(const char *, q));
  1223. default:
  1224. break;
  1225. }
  1226. @@ -307,13 +307,13 @@
  1227. s++;
  1228. goto val;
  1229. default:
  1230. - if (!isdigit((unsigned char)*s))
  1231. + if (!isdigit(CAST(unsigned char, *s)))
  1232. return 0;
  1233. slen = 0;
  1234. do
  1235. slen = slen * 10 + *s - '0';
  1236. - while (isdigit((unsigned char)*++s));
  1237. + while (isdigit(CAST(unsigned char, *++s)));
  1238. if ((ms->flags & MAGIC_DEBUG) != 0)
  1239. fprintf(stderr, "%s: len %" SIZE_T_FORMAT "u %u\n",
  1240. __func__, slen, tlen);
  1241. --- a/src/elfclass.h
  1242. +++ b/src/elfclass.h
  1243. @@ -41,8 +41,8 @@
  1244. return toomany(ms, "program headers", phnum);
  1245. flags |= FLAGS_IS_CORE;
  1246. if (dophn_core(ms, clazz, swap, fd,
  1247. - (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
  1248. - (size_t)elf_getu16(swap, elfhdr.e_phentsize),
  1249. + CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
  1250. + CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),
  1251. fsize, &flags, &notecount) == -1)
  1252. return -1;
  1253. break;
  1254. @@ -56,8 +56,8 @@
  1255. if (shnum > ms->elf_shnum_max)
  1256. return toomany(ms, "section", shnum);
  1257. if (dophn_exec(ms, clazz, swap, fd,
  1258. - (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
  1259. - (size_t)elf_getu16(swap, elfhdr.e_phentsize),
  1260. + CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
  1261. + CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),
  1262. fsize, shnum, &flags, &notecount) == -1)
  1263. return -1;
  1264. /*FALLTHROUGH*/
  1265. @@ -66,10 +66,10 @@
  1266. if (shnum > ms->elf_shnum_max)
  1267. return toomany(ms, "section headers", shnum);
  1268. if (doshn(ms, clazz, swap, fd,
  1269. - (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
  1270. - (size_t)elf_getu16(swap, elfhdr.e_shentsize),
  1271. + CAST(off_t, elf_getu(swap, elfhdr.e_shoff)), shnum,
  1272. + CAST(size_t, elf_getu16(swap, elfhdr.e_shentsize)),
  1273. fsize, elf_getu16(swap, elfhdr.e_machine),
  1274. - (int)elf_getu16(swap, elfhdr.e_shstrndx),
  1275. + CAST(int, elf_getu16(swap, elfhdr.e_shstrndx)),
  1276. &flags, &notecount) == -1)
  1277. return -1;
  1278. break;
  1279. --- a/src/encoding.c
  1280. +++ b/src/encoding.c
  1281. @@ -89,12 +89,12 @@
  1282. *code_mime = "binary";
  1283. mlen = (nbytes + 1) * sizeof((*ubuf)[0]);
  1284. - if ((*ubuf = CAST(unichar *, calloc((size_t)1, mlen))) == NULL) {
  1285. + if ((*ubuf = CAST(unichar *, calloc(CAST(size_t, 1), mlen))) == NULL) {
  1286. file_oomem(ms, mlen);
  1287. goto done;
  1288. }
  1289. mlen = (nbytes + 1) * sizeof(nbuf[0]);
  1290. - if ((nbuf = CAST(unsigned char *, calloc((size_t)1, mlen))) == NULL) {
  1291. + if ((nbuf = CAST(unsigned char *, calloc(CAST(size_t, 1), mlen))) == NULL) {
  1292. file_oomem(ms, mlen);
  1293. goto done;
  1294. }
  1295. @@ -449,7 +449,7 @@
  1296. if (ubf[*ulen - 1] == 0xfffe)
  1297. return 0;
  1298. if (ubf[*ulen - 1] < 128 &&
  1299. - text_chars[(size_t)ubf[*ulen - 1]] != T)
  1300. + text_chars[CAST(size_t, ubf[*ulen - 1])] != T)
  1301. return 0;
  1302. }
  1303. @@ -488,7 +488,7 @@
  1304. if (ubf[*ulen - 1] == 0xfffe)
  1305. return 0;
  1306. if (ubf[*ulen - 1] < 128 &&
  1307. - text_chars[(size_t)ubf[*ulen - 1]] != T)
  1308. + text_chars[CAST(size_t, ubf[*ulen - 1])] != T)
  1309. return 0;
  1310. }
  1311. --- a/src/file.c
  1312. +++ b/src/file.c
  1313. @@ -400,7 +400,8 @@
  1314. }
  1315. else {
  1316. size_t j, wid, nw;
  1317. - for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
  1318. + for (wid = 0, j = CAST(size_t, optind); j < CAST(size_t, argc);
  1319. + j++) {
  1320. nw = file_mbswidth(argv[j]);
  1321. if (nw > wid)
  1322. wid = nw;
  1323. @@ -537,9 +538,8 @@
  1324. (void)putc('\0', stdout);
  1325. if (nulsep < 2) {
  1326. (void)printf("%s", separator);
  1327. - (void)printf("%*s ",
  1328. - (int) (nopad ? 0 : (wid - file_mbswidth(inname))),
  1329. - "");
  1330. + (void)printf("%*s ", CAST(int, nopad ? 0
  1331. + : (wid - file_mbswidth(inname))), "");
  1332. }
  1333. }
  1334. @@ -566,8 +566,8 @@
  1335. while (n > 0) {
  1336. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  1337. - if (bytesconsumed == (size_t)(-1) ||
  1338. - bytesconsumed == (size_t)(-2)) {
  1339. + if (bytesconsumed == CAST(size_t, -1) ||
  1340. + bytesconsumed == CAST(size_t, -2)) {
  1341. /* Something went wrong, return something reasonable */
  1342. return old_n;
  1343. }
  1344. @@ -626,13 +626,13 @@
  1345. for (sp = p - 1; sp > opts && *sp == ' '; sp--)
  1346. continue;
  1347. - fprintf(stdout, "%.*s", (int)(p - opts), opts);
  1348. + fprintf(stdout, "%.*s", CAST(int, p - opts), opts);
  1349. comma = 0;
  1350. for (i = 0; i < __arraycount(nv); i++) {
  1351. fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
  1352. if (i && i % 5 == 0 && i != __arraycount(nv) - 1) {
  1353. - fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
  1354. + fprintf(stdout, ",\n%*s", CAST(int, p - sp - 1), "");
  1355. comma = 0;
  1356. }
  1357. }
  1358. --- a/src/funcs.c
  1359. +++ b/src/funcs.c
  1360. @@ -392,9 +392,9 @@
  1361. #define OCTALIFY(n, o) \
  1362. /*LINTED*/ \
  1363. (void)(*(n)++ = '\\', \
  1364. - *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
  1365. - *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
  1366. - *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
  1367. + *(n)++ = ((CAST(uint32_t, *(o)) >> 6) & 3) + '0', \
  1368. + *(n)++ = ((CAST(uint32_t, *(o)) >> 3) & 7) + '0', \
  1369. + *(n)++ = ((CAST(uint32_t, *(o)) >> 0) & 7) + '0', \
  1370. (o)++)
  1371. protected const char *
  1372. @@ -440,9 +440,9 @@
  1373. while (op < eop) {
  1374. bytesconsumed = mbrtowc(&nextchar, op,
  1375. - (size_t)(eop - op), &state);
  1376. - if (bytesconsumed == (size_t)(-1) ||
  1377. - bytesconsumed == (size_t)(-2)) {
  1378. + CAST(size_t, eop - op), &state);
  1379. + if (bytesconsumed == CAST(size_t, -1) ||
  1380. + bytesconsumed == CAST(size_t, -2)) {
  1381. mb_conv = 0;
  1382. break;
  1383. }
  1384. @@ -465,7 +465,7 @@
  1385. #endif
  1386. for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
  1387. - if (isprint((unsigned char)*op)) {
  1388. + if (isprint(CAST(unsigned char, *op))) {
  1389. *np++ = *op++;
  1390. } else {
  1391. OCTALIFY(np, op);
  1392. @@ -626,7 +626,7 @@
  1393. file_printable(char *buf, size_t bufsiz, const char *str, size_t slen)
  1394. {
  1395. char *ptr, *eptr = buf + bufsiz - 1;
  1396. - const unsigned char *s = (const unsigned char *)str;
  1397. + const unsigned char *s = RCAST(const unsigned char *, str);
  1398. const unsigned char *es = s + slen;
  1399. for (ptr = buf; ptr < eptr && s < es && *s; s++) {
  1400. --- a/src/is_tar.c
  1401. +++ b/src/is_tar.c
  1402. @@ -40,7 +40,7 @@
  1403. #include "file.h"
  1404. #ifndef lint
  1405. -FILE_RCSID("@(#)$File: is_tar.c,v 1.43 2018/10/15 16:29:16 christos Exp $")
  1406. +FILE_RCSID("@(#)$File: is_tar.c,v 1.44 2019/02/20 02:35:27 christos Exp $")
  1407. #endif
  1408. #include "magic.h"
  1409. @@ -98,7 +98,8 @@
  1410. private int
  1411. is_tar(const unsigned char *buf, size_t nbytes)
  1412. {
  1413. - const union record *header = (const union record *)(const void *)buf;
  1414. + const union record *header = RCAST(const union record *,
  1415. + RCAST(const void *, buf));
  1416. size_t i;
  1417. int sum, recsum;
  1418. const unsigned char *p, *ep;
  1419. @@ -147,7 +148,7 @@
  1420. if (digs == 0)
  1421. return -1;
  1422. - while (isspace((unsigned char)*where)) { /* Skip spaces */
  1423. + while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */
  1424. where++;
  1425. if (digs-- == 0)
  1426. return -1; /* All blank field */
  1427. @@ -158,7 +159,7 @@
  1428. digs--;
  1429. }
  1430. - if (digs > 0 && *where && !isspace((unsigned char)*where))
  1431. + if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))
  1432. return -1; /* Ended on non-(space/NUL) */
  1433. return value;
  1434. --- a/src/magic.c
  1435. +++ b/src/magic.c
  1436. @@ -312,7 +312,8 @@
  1437. {
  1438. if (ms == NULL)
  1439. return -1;
  1440. - return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
  1441. + return buffer_apprentice(ms, RCAST(struct magic **, bufs),
  1442. + sizes, nbufs);
  1443. }
  1444. #endif
  1445. @@ -405,7 +406,7 @@
  1446. struct stat sb;
  1447. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  1448. int ispipe = 0;
  1449. - off_t pos = (off_t)-1;
  1450. + off_t pos = CAST(off_t, -1);
  1451. if (file_reset(ms, 1) == -1)
  1452. goto out;
  1453. @@ -464,7 +465,7 @@
  1454. if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
  1455. ispipe = 1;
  1456. if (inname == NULL)
  1457. - pos = lseek(fd, (off_t)0, SEEK_CUR);
  1458. + pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
  1459. }
  1460. /*
  1461. @@ -473,8 +474,8 @@
  1462. if (ispipe) {
  1463. ssize_t r = 0;
  1464. - while ((r = sread(fd, (void *)&buf[nbytes],
  1465. - (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
  1466. + while ((r = sread(fd, RCAST(void *, &buf[nbytes]),
  1467. + CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) {
  1468. nbytes += r;
  1469. if (r < PIPE_BUF) break;
  1470. }
  1471. @@ -494,7 +495,7 @@
  1472. _isatty(fd) ? 8 * 1024 :
  1473. #endif
  1474. ms->bytes_max;
  1475. - if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
  1476. + if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) {
  1477. if (inname == NULL && fd != STDIN_FILENO)
  1478. file_error(ms, errno, "cannot read fd %d", fd);
  1479. else
  1480. @@ -505,13 +506,13 @@
  1481. }
  1482. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  1483. - if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
  1484. + if (file_buffer(ms, fd, inname, buf, CAST(size_t, nbytes)) == -1)
  1485. goto done;
  1486. rv = 0;
  1487. done:
  1488. free(buf);
  1489. if (fd != -1) {
  1490. - if (pos != (off_t)-1)
  1491. + if (pos != CAST(off_t, -1))
  1492. (void)lseek(fd, pos, SEEK_SET);
  1493. close_and_restore(ms, inname, fd, &sb);
  1494. }
  1495. @@ -589,25 +590,25 @@
  1496. return -1;
  1497. switch (param) {
  1498. case MAGIC_PARAM_INDIR_MAX:
  1499. - ms->indir_max = (uint16_t)*(const size_t *)val;
  1500. + ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
  1501. return 0;
  1502. case MAGIC_PARAM_NAME_MAX:
  1503. - ms->name_max = (uint16_t)*(const size_t *)val;
  1504. + ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
  1505. return 0;
  1506. case MAGIC_PARAM_ELF_PHNUM_MAX:
  1507. - ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
  1508. + ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  1509. return 0;
  1510. case MAGIC_PARAM_ELF_SHNUM_MAX:
  1511. - ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
  1512. + ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  1513. return 0;
  1514. case MAGIC_PARAM_ELF_NOTES_MAX:
  1515. - ms->elf_notes_max = (uint16_t)*(const size_t *)val;
  1516. + ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
  1517. return 0;
  1518. case MAGIC_PARAM_REGEX_MAX:
  1519. - ms->regex_max = (uint16_t)*(const size_t *)val;
  1520. + ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
  1521. return 0;
  1522. case MAGIC_PARAM_BYTES_MAX:
  1523. - ms->bytes_max = *(const size_t *)val;
  1524. + ms->bytes_max = *CAST(const size_t *, val);
  1525. return 0;
  1526. default:
  1527. errno = EINVAL;
  1528. @@ -622,25 +623,25 @@
  1529. return -1;
  1530. switch (param) {
  1531. case MAGIC_PARAM_INDIR_MAX:
  1532. - *(size_t *)val = ms->indir_max;
  1533. + *CAST(size_t *, val) = ms->indir_max;
  1534. return 0;
  1535. case MAGIC_PARAM_NAME_MAX:
  1536. - *(size_t *)val = ms->name_max;
  1537. + *CAST(size_t *, val) = ms->name_max;
  1538. return 0;
  1539. case MAGIC_PARAM_ELF_PHNUM_MAX:
  1540. - *(size_t *)val = ms->elf_phnum_max;
  1541. + *CAST(size_t *, val) = ms->elf_phnum_max;
  1542. return 0;
  1543. case MAGIC_PARAM_ELF_SHNUM_MAX:
  1544. - *(size_t *)val = ms->elf_shnum_max;
  1545. + *CAST(size_t *, val) = ms->elf_shnum_max;
  1546. return 0;
  1547. case MAGIC_PARAM_ELF_NOTES_MAX:
  1548. - *(size_t *)val = ms->elf_notes_max;
  1549. + *CAST(size_t *, val) = ms->elf_notes_max;
  1550. return 0;
  1551. case MAGIC_PARAM_REGEX_MAX:
  1552. - *(size_t *)val = ms->regex_max;
  1553. + *CAST(size_t *, val) = ms->regex_max;
  1554. return 0;
  1555. case MAGIC_PARAM_BYTES_MAX:
  1556. - *(size_t *)val = ms->bytes_max;
  1557. + *CAST(size_t *, val) = ms->bytes_max;
  1558. return 0;
  1559. default:
  1560. errno = EINVAL;
  1561. --- a/src/print.c
  1562. +++ b/src/print.c
  1563. @@ -32,7 +32,7 @@
  1564. #include "file.h"
  1565. #ifndef lint
  1566. -FILE_RCSID("@(#)$File: print.c,v 1.83 2018/09/09 20:33:28 christos Exp $")
  1567. +FILE_RCSID("@(#)$File: print.c,v 1.84 2019/02/20 02:35:27 christos Exp $")
  1568. #endif /* lint */
  1569. #include <string.h>
  1570. @@ -65,7 +65,7 @@
  1571. if (m->in_op & FILE_OPINVERSE)
  1572. (void) fputc('~', stderr);
  1573. (void) fprintf(stderr, "%c%u),",
  1574. - ((size_t)(m->in_op & FILE_OPS_MASK) <
  1575. + (CAST(size_t, m->in_op & FILE_OPS_MASK) <
  1576. SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
  1577. m->in_offset);
  1578. }
  1579. @@ -112,14 +112,14 @@
  1580. (void) fprintf(stderr, "/%u", m->str_range);
  1581. }
  1582. else {
  1583. - if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
  1584. + if (CAST(size_t, m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
  1585. (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
  1586. else
  1587. (void) fputc('?', stderr);
  1588. if (m->num_mask) {
  1589. (void) fprintf(stderr, "%.8llx",
  1590. - (unsigned long long)m->num_mask);
  1591. + CAST(unsigned long long, m->num_mask));
  1592. }
  1593. }
  1594. (void) fprintf(stderr, ",%c", m->reln);
  1595. @@ -141,7 +141,7 @@
  1596. case FILE_LEQUAD:
  1597. case FILE_QUAD:
  1598. (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
  1599. - (unsigned long long)m->value.q);
  1600. + CAST(long long, m->value.q));
  1601. break;
  1602. case FILE_PSTRING:
  1603. case FILE_STRING:
  1604. @@ -149,7 +149,8 @@
  1605. case FILE_BESTRING16:
  1606. case FILE_LESTRING16:
  1607. case FILE_SEARCH:
  1608. - file_showstr(stderr, m->value.s, (size_t)m->vallen);
  1609. + file_showstr(stderr, m->value.s,
  1610. + CAST(size_t, m->vallen));
  1611. break;
  1612. case FILE_DATE:
  1613. case FILE_LEDATE:
  1614. @@ -221,7 +222,7 @@
  1615. if (ms->file)
  1616. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  1617. - (unsigned long)ms->line);
  1618. + CAST(unsigned long, ms->line));
  1619. (void) fprintf(stderr, "Warning: ");
  1620. va_start(va, f);
  1621. (void) vfprintf(stderr, f, va);
  1622. @@ -243,7 +244,7 @@
  1623. } else {
  1624. // XXX: perhaps detect and print something if overflow
  1625. // on 32 bit time_t?
  1626. - t = (time_t)v;
  1627. + t = CAST(time_t, v);
  1628. }
  1629. if (flags & FILE_T_LOCAL) {
  1630. --- a/src/readcdf.c
  1631. +++ b/src/readcdf.c
  1632. @@ -204,7 +204,7 @@
  1633. && len--; s += k) {
  1634. if (*s == '\0')
  1635. break;
  1636. - if (isprint((unsigned char)*s))
  1637. + if (isprint(CAST(unsigned char, *s)))
  1638. vbuf[j++] = *s;
  1639. }
  1640. if (j == sizeof(vbuf))
  1641. @@ -318,19 +318,19 @@
  1642. case 2:
  1643. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  1644. si.si_os_version & 0xff,
  1645. - (uint32_t)si.si_os_version >> 8) == -1)
  1646. + CAST(uint32_t, si.si_os_version) >> 8) == -1)
  1647. return -2;
  1648. break;
  1649. case 1:
  1650. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  1651. - (uint32_t)si.si_os_version >> 8,
  1652. + CAST(uint32_t, si.si_os_version) >> 8,
  1653. si.si_os_version & 0xff) == -1)
  1654. return -2;
  1655. break;
  1656. default:
  1657. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  1658. si.si_os_version & 0xff,
  1659. - (uint32_t)si.si_os_version >> 8) == -1)
  1660. + CAST(uint32_t, si.si_os_version) >> 8) == -1)
  1661. return -2;
  1662. break;
  1663. }
  1664. @@ -406,7 +406,7 @@
  1665. for (j = 0; str == NULL && j < dir->dir_len; j++) {
  1666. d = &dir->dir_tab[j];
  1667. for (k = 0; k < sizeof(name); k++)
  1668. - name[k] = (char)cdf_tole2(d->d_name[k]);
  1669. + name[k] = CAST(char, cdf_tole2(d->d_name[k]));
  1670. str = cdf_app_to_mime(name,
  1671. NOTMIME(ms) ? name2desc : name2mime);
  1672. }
  1673. --- a/src/readelf.c
  1674. +++ b/src/readelf.c
  1675. @@ -393,9 +393,9 @@
  1676. }
  1677. offset = 0;
  1678. for (;;) {
  1679. - if (offset >= (size_t)bufsize)
  1680. + if (offset >= CAST(size_t, bufsize))
  1681. break;
  1682. - offset = donote(ms, nbuf, offset, (size_t)bufsize,
  1683. + offset = donote(ms, nbuf, offset, CAST(size_t, bufsize),
  1684. clazz, swap, 4, flags, notecount, fd, ph_off,
  1685. ph_num, fsize);
  1686. if (offset == 0)
  1687. @@ -533,7 +533,7 @@
  1688. int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
  1689. size_t noff, size_t doff, int *flags)
  1690. {
  1691. - if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  1692. + if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "GNU") == 0 &&
  1693. type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
  1694. uint8_t desc[20];
  1695. const char *btype;
  1696. @@ -561,10 +561,10 @@
  1697. return 1;
  1698. return 1;
  1699. }
  1700. - if (namesz == 4 && strcmp((char *)&nbuf[noff], "Go") == 0 &&
  1701. + if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 &&
  1702. type == NT_GO_BUILD_ID && descsz < 128) {
  1703. if (file_printf(ms, ", Go BuildID=%.*s",
  1704. - CAST(int, descsz), CAST(char *, &nbuf[doff])) == -1)
  1705. + CAST(int, descsz), RCAST(char *, &nbuf[doff])) == -1)
  1706. return -1;
  1707. return 1;
  1708. }
  1709. @@ -576,7 +576,9 @@
  1710. int swap, uint32_t namesz, uint32_t descsz,
  1711. size_t noff, size_t doff, int *flags)
  1712. {
  1713. - if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  1714. + const char *name = RCAST(const char *, &nbuf[noff]);
  1715. +
  1716. + if (namesz == 5 && strcmp(name, "SuSE") == 0 &&
  1717. type == NT_GNU_VERSION && descsz == 2) {
  1718. *flags |= FLAGS_DID_OS_NOTE;
  1719. if (file_printf(ms, ", for SuSE %d.%d", nbuf[doff],
  1720. @@ -585,7 +587,7 @@
  1721. return 1;
  1722. }
  1723. - if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  1724. + if (namesz == 4 && strcmp(name, "GNU") == 0 &&
  1725. type == NT_GNU_VERSION && descsz == 16) {
  1726. uint32_t desc[4];
  1727. memcpy(desc, &nbuf[doff], sizeof(desc));
  1728. @@ -624,7 +626,7 @@
  1729. return 1;
  1730. }
  1731. - if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  1732. + if (namesz == 7 && strcmp(name, "NetBSD") == 0) {
  1733. if (type == NT_NETBSD_VERSION && descsz == 4) {
  1734. *flags |= FLAGS_DID_OS_NOTE;
  1735. do_note_netbsd_version(ms, swap, &nbuf[doff]);
  1736. @@ -632,7 +634,7 @@
  1737. }
  1738. }
  1739. - if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
  1740. + if (namesz == 8 && strcmp(name, "FreeBSD") == 0) {
  1741. if (type == NT_FREEBSD_VERSION && descsz == 4) {
  1742. *flags |= FLAGS_DID_OS_NOTE;
  1743. do_note_freebsd_version(ms, swap, &nbuf[doff]);
  1744. @@ -640,7 +642,7 @@
  1745. }
  1746. }
  1747. - if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  1748. + if (namesz == 8 && strcmp(name, "OpenBSD") == 0 &&
  1749. type == NT_OPENBSD_VERSION && descsz == 4) {
  1750. *flags |= FLAGS_DID_OS_NOTE;
  1751. if (file_printf(ms, ", for OpenBSD") == -1)
  1752. @@ -649,7 +651,7 @@
  1753. return 1;
  1754. }
  1755. - if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  1756. + if (namesz == 10 && strcmp(name, "DragonFly") == 0 &&
  1757. type == NT_DRAGONFLY_VERSION && descsz == 4) {
  1758. uint32_t desc;
  1759. *flags |= FLAGS_DID_OS_NOTE;
  1760. @@ -670,7 +672,9 @@
  1761. int swap, uint32_t namesz, uint32_t descsz,
  1762. size_t noff, size_t doff, int *flags)
  1763. {
  1764. - if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
  1765. + const char *name = RCAST(const char *, &nbuf[noff]);
  1766. +
  1767. + if (namesz == 4 && strcmp(name, "PaX") == 0 &&
  1768. type == NT_NETBSD_PAX && descsz == 4) {
  1769. static const char *pax[] = {
  1770. "+mprotect",
  1771. @@ -692,7 +696,7 @@
  1772. return 1;
  1773. for (i = 0; i < __arraycount(pax); i++) {
  1774. - if (((1 << (int)i) & desc) == 0)
  1775. + if (((1 << CAST(int, i)) & desc) == 0)
  1776. continue;
  1777. if (file_printf(ms, "%s%s", did++ ? "," : "",
  1778. pax[i]) == -1)
  1779. @@ -709,6 +713,8 @@
  1780. size_t noff, size_t doff, int *flags, size_t size, int clazz)
  1781. {
  1782. #ifdef ELFCORE
  1783. + const char *name = RCAST(const char *, &nbuf[noff]);
  1784. +
  1785. int os_style = -1;
  1786. /*
  1787. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  1788. @@ -724,16 +730,16 @@
  1789. * doesn't include the terminating null in the
  1790. * name....
  1791. */
  1792. - if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  1793. - (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  1794. + if ((namesz == 4 && strncmp(name, "CORE", 4) == 0) ||
  1795. + (namesz == 5 && strcmp(name, "CORE") == 0)) {
  1796. os_style = OS_STYLE_SVR4;
  1797. }
  1798. - if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  1799. + if ((namesz == 8 && strcmp(name, "FreeBSD") == 0)) {
  1800. os_style = OS_STYLE_FREEBSD;
  1801. }
  1802. - if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  1803. + if ((namesz >= 11 && strncmp(name, "NetBSD-CORE", 11)
  1804. == 0)) {
  1805. os_style = OS_STYLE_NETBSD;
  1806. }
  1807. @@ -758,11 +764,11 @@
  1808. "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
  1809. file_printable(sbuf, sizeof(sbuf),
  1810. RCAST(char *, pi.cpi_name), sizeof(pi.cpi_name)),
  1811. - elf_getu32(swap, (uint32_t)pi.cpi_pid),
  1812. + elf_getu32(swap, CAST(uint32_t, pi.cpi_pid)),
  1813. elf_getu32(swap, pi.cpi_euid),
  1814. elf_getu32(swap, pi.cpi_egid),
  1815. elf_getu32(swap, pi.cpi_nlwps),
  1816. - elf_getu32(swap, (uint32_t)pi.cpi_siglwp),
  1817. + elf_getu32(swap, CAST(uint32_t, pi.cpi_siglwp)),
  1818. elf_getu32(swap, pi.cpi_signo),
  1819. elf_getu32(swap, pi.cpi_sigcode)) == -1)
  1820. return 1;
  1821. @@ -876,8 +882,8 @@
  1822. i = k;
  1823. }
  1824. - cname = (unsigned char *)
  1825. - &nbuf[doff + prpsoffsets(i)];
  1826. + cname = CAST(unsigned char *,
  1827. + &nbuf[doff + prpsoffsets(i)]);
  1828. for (cp = cname; cp < nbuf + size && *cp
  1829. && isprint(*cp); cp++)
  1830. continue;
  1831. @@ -888,7 +894,7 @@
  1832. while (cp > cname && isspace(cp[-1]))
  1833. cp--;
  1834. if (file_printf(ms, ", from '%.*s'",
  1835. - (int)(cp - cname), cname) == -1)
  1836. + CAST(int, cp - cname), cname) == -1)
  1837. return 1;
  1838. *flags |= FLAGS_DID_CORE;
  1839. return 1;
  1840. @@ -915,7 +921,8 @@
  1841. * virtual address in which the "virtaddr" belongs to.
  1842. */
  1843. for ( ; num; num--) {
  1844. - if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  1845. + if (pread(fd, xph_addr, xph_sizeof, off) <
  1846. + CAST(ssize_t, xph_sizeof)) {
  1847. file_badread(ms);
  1848. return -1;
  1849. }
  1850. @@ -955,7 +962,7 @@
  1851. /* We expect only printable characters, so return if buffer contains
  1852. * non-printable character before the '\0' or just '\0'. */
  1853. - for (bptr = buf; *bptr && isprint((unsigned char)*bptr); bptr++)
  1854. + for (bptr = buf; *bptr && isprint(CAST(unsigned char, *bptr)); bptr++)
  1855. continue;
  1856. if (*bptr != '\0')
  1857. return 0;
  1858. @@ -1061,8 +1068,8 @@
  1859. if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
  1860. return 0;
  1861. } else {
  1862. - if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val)
  1863. - == -1)
  1864. + if (file_printf(ms, ", %s: %d", tag,
  1865. + CAST(int, xauxv_val)) == -1)
  1866. return 0;
  1867. }
  1868. }
  1869. @@ -1656,7 +1663,7 @@
  1870. case PT_DYNAMIC:
  1871. offset = 0;
  1872. for (;;) {
  1873. - if (offset >= (size_t)bufsize)
  1874. + if (offset >= CAST(size_t, bufsize))
  1875. break;
  1876. offset = dodynamic(ms, nbuf, offset,
  1877. CAST(size_t, bufsize), clazz, swap);
  1878. @@ -1668,7 +1675,7 @@
  1879. case PT_INTERP:
  1880. if (bufsize && nbuf[0]) {
  1881. nbuf[bufsize - 1] = '\0';
  1882. - memcpy(interp, nbuf, (size_t)bufsize);
  1883. + memcpy(interp, nbuf, CAST(size_t, bufsize));
  1884. } else
  1885. strlcpy(interp, "*empty*", sizeof(interp));
  1886. break;
  1887. @@ -1679,7 +1686,7 @@
  1888. */
  1889. offset = 0;
  1890. for (;;) {
  1891. - if (offset >= (size_t)bufsize)
  1892. + if (offset >= CAST(size_t, bufsize))
  1893. break;
  1894. offset = donote(ms, nbuf, offset,
  1895. CAST(size_t, bufsize), clazz, swap, align,
  1896. --- a/src/softmagic.c
  1897. +++ b/src/softmagic.c
  1898. @@ -67,24 +67,46 @@
  1899. private int cvt_32(union VALUETYPE *, const struct magic *);
  1900. private int cvt_64(union VALUETYPE *, const struct magic *);
  1901. -#define OFFSET_OOB(n, o, i) ((n) < (uint32_t)(o) || (i) > ((n) - (o)))
  1902. -#define BE64(p) (((uint64_t)(p)->hq[0]<<56)|((uint64_t)(p)->hq[1]<<48)| \
  1903. - ((uint64_t)(p)->hq[2]<<40)|((uint64_t)(p)->hq[3]<<32)| \
  1904. - ((uint64_t)(p)->hq[4]<<24)|((uint64_t)(p)->hq[5]<<16)| \
  1905. - ((uint64_t)(p)->hq[6]<<8)|((uint64_t)(p)->hq[7]))
  1906. -#define LE64(p) (((uint64_t)(p)->hq[7]<<56)|((uint64_t)(p)->hq[6]<<48)| \
  1907. - ((uint64_t)(p)->hq[5]<<40)|((uint64_t)(p)->hq[4]<<32)| \
  1908. - ((uint64_t)(p)->hq[3]<<24)|((uint64_t)(p)->hq[2]<<16)| \
  1909. - ((uint64_t)(p)->hq[1]<<8)|((uint64_t)(p)->hq[0]))
  1910. -#define LE32(p) (((uint32_t)(p)->hl[3]<<24)|((uint32_t)(p)->hl[2]<<16)| \
  1911. - ((uint32_t)(p)->hl[1]<<8)|((uint32_t)(p)->hl[0]))
  1912. -#define BE32(p) (((uint32_t)(p)->hl[0]<<24)|((uint32_t)(p)->hl[1]<<16)| \
  1913. - ((uint32_t)(p)->hl[2]<<8)|((uint32_t)(p)->hl[3]))
  1914. -#define ME32(p) (((uint32_t)(p)->hl[1]<<24)|((uint32_t)(p)->hl[0]<<16)| \
  1915. - ((uint32_t)(p)->hl[3]<<8)|((uint32_t)(p)->hl[2]))
  1916. -#define BE16(p) (((uint16_t)(p)->hs[0]<<8)|((uint16_t)(p)->hs[1]))
  1917. -#define LE16(p) (((uint16_t)(p)->hs[1]<<8)|((uint16_t)(p)->hs[0]))
  1918. -#define SEXT(s,v,p) ((s)?(intmax_t)(int##v##_t)(p):(intmax_t)(uint##v##_t)(p))
  1919. +#define OFFSET_OOB(n, o, i) ((n) < CAST(uint32_t, (o)) || (i) > ((n) - (o)))
  1920. +#define BE64(p) ( \
  1921. + (CAST(uint64_t, (p)->hq[0])<<56)| \
  1922. + (CAST(uint64_t, (p)->hq[1])<<48)| \
  1923. + (CAST(uint64_t, (p)->hq[2])<<40)| \
  1924. + (CAST(uint64_t, (p)->hq[3])<<32)| \
  1925. + (CAST(uint64_t, (p)->hq[4])<<24)| \
  1926. + (CAST(uint64_t, (p)->hq[5])<<16)| \
  1927. + (CAST(uint64_t, (p)->hq[6])<<8)| \
  1928. + (CAST(uint64_t, (p)->hq[7])))
  1929. +#define LE64(p) ( \
  1930. + (CAST(uint64_t, (p)->hq[7])<<56)| \
  1931. + (CAST(uint64_t, (p)->hq[6])<<48)| \
  1932. + (CAST(uint64_t, (p)->hq[5])<<40)| \
  1933. + (CAST(uint64_t, (p)->hq[4])<<32)| \
  1934. + (CAST(uint64_t, (p)->hq[3])<<24)| \
  1935. + (CAST(uint64_t, (p)->hq[2])<<16)| \
  1936. + (CAST(uint64_t, (p)->hq[1])<<8)| \
  1937. + (CAST(uint64_t, (p)->hq[0])))
  1938. +#define LE32(p) ( \
  1939. + (CAST(uint32_t, (p)->hl[3])<<24)| \
  1940. + (CAST(uint32_t, (p)->hl[2])<<16)| \
  1941. + (CAST(uint32_t, (p)->hl[1])<<8)| \
  1942. + (CAST(uint32_t, (p)->hl[0])))
  1943. +#define BE32(p) ( \
  1944. + (CAST(uint32_t, (p)->hl[0])<<24)| \
  1945. + (CAST(uint32_t, (p)->hl[1])<<16)| \
  1946. + (CAST(uint32_t, (p)->hl[2])<<8)| \
  1947. + (CAST(uint32_t, (p)->hl[3])))
  1948. +#define ME32(p) ( \
  1949. + (CAST(uint32_t, (p)->hl[1])<<24)| \
  1950. + (CAST(uint32_t, (p)->hl[0])<<16)| \
  1951. + (CAST(uint32_t, (p)->hl[3])<<8)| \
  1952. + (CAST(uint32_t, (p)->hl[2])))
  1953. +
  1954. +#define BE16(p) ((CAST(uint16_t, (p)->hs[0])<<8)|(CAST(uint16_t, (p)->hs[1])))
  1955. +#define LE16(p) ((CAST(uint16_t, (p)->hs[1])<<8)|(CAST(uint16_t, (p)->hs[0])))
  1956. +#define SEXT(s,v,p) ((s) ? \
  1957. + CAST(intmax_t, CAST(int##v##_t, p)) : \
  1958. + CAST(intmax_t, CAST(uint##v##_t, p)))
  1959. /*
  1960. * softmagic - lookup one file in parsed, in-memory copy of database
  1961. @@ -478,7 +500,7 @@
  1962. size_t l;
  1963. for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
  1964. - l = (size_t)(ptr - sptr);
  1965. + l = CAST(size_t, ptr - sptr);
  1966. if (l >= len)
  1967. return -1;
  1968. memcpy(buf, sptr, l);
  1969. @@ -544,19 +566,19 @@
  1970. switch (m->type) {
  1971. case FILE_BYTE:
  1972. - v = file_signextend(ms, m, (uint64_t)p->b);
  1973. + v = file_signextend(ms, m, CAST(uint64_t, p->b));
  1974. switch (check_fmt(ms, desc)) {
  1975. case -1:
  1976. return -1;
  1977. case 1:
  1978. (void)snprintf(buf, sizeof(buf), "%d",
  1979. - (unsigned char)v);
  1980. + CAST(unsigned char, v));
  1981. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  1982. return -1;
  1983. break;
  1984. default:
  1985. if (file_printf(ms, F(ms, desc, "%d"),
  1986. - (unsigned char) v) == -1)
  1987. + CAST(unsigned char, v)) == -1)
  1988. return -1;
  1989. break;
  1990. }
  1991. @@ -566,19 +588,19 @@
  1992. case FILE_SHORT:
  1993. case FILE_BESHORT:
  1994. case FILE_LESHORT:
  1995. - v = file_signextend(ms, m, (uint64_t)p->h);
  1996. + v = file_signextend(ms, m, CAST(uint64_t, p->h));
  1997. switch (check_fmt(ms, desc)) {
  1998. case -1:
  1999. return -1;
  2000. case 1:
  2001. (void)snprintf(buf, sizeof(buf), "%u",
  2002. - (unsigned short)v);
  2003. + CAST(unsigned short, v));
  2004. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  2005. return -1;
  2006. break;
  2007. default:
  2008. if (file_printf(ms, F(ms, desc, "%u"),
  2009. - (unsigned short) v) == -1)
  2010. + CAST(unsigned short, v)) == -1)
  2011. return -1;
  2012. break;
  2013. }
  2014. @@ -589,17 +611,19 @@
  2015. case FILE_BELONG:
  2016. case FILE_LELONG:
  2017. case FILE_MELONG:
  2018. - v = file_signextend(ms, m, (uint64_t)p->l);
  2019. + v = file_signextend(ms, m, CAST(uint64_t, p->l));
  2020. switch (check_fmt(ms, desc)) {
  2021. case -1:
  2022. return -1;
  2023. case 1:
  2024. - (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
  2025. + (void)snprintf(buf, sizeof(buf), "%u",
  2026. + CAST(uint32_t, v));
  2027. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  2028. return -1;
  2029. break;
  2030. default:
  2031. - if (file_printf(ms, F(ms, desc, "%u"), (uint32_t) v) == -1)
  2032. + if (file_printf(ms, F(ms, desc, "%u"),
  2033. + CAST(uint32_t, v)) == -1)
  2034. return -1;
  2035. break;
  2036. }
  2037. @@ -615,13 +639,13 @@
  2038. return -1;
  2039. case 1:
  2040. (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
  2041. - (unsigned long long)v);
  2042. + CAST(unsigned long long, v));
  2043. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  2044. return -1;
  2045. break;
  2046. default:
  2047. if (file_printf(ms, F(ms, desc, "%" INT64_T_FORMAT "u"),
  2048. - (unsigned long long) v) == -1)
  2049. + CAST(unsigned long long, v)) == -1)
  2050. return -1;
  2051. break;
  2052. }
  2053. @@ -650,13 +674,13 @@
  2054. if (m->str_flags & STRING_TRIM) {
  2055. char *last;
  2056. - while (isspace((unsigned char)*str))
  2057. + while (isspace(CAST(unsigned char, *str)))
  2058. str++;
  2059. last = str;
  2060. while (*last)
  2061. last++;
  2062. --last;
  2063. - while (isspace((unsigned char)*last))
  2064. + while (isspace(CAST(unsigned char, *last)))
  2065. last--;
  2066. *++last = '\0';
  2067. }
  2068. @@ -763,7 +787,8 @@
  2069. char *cp;
  2070. int rval;
  2071. - cp = strndup((const char *)ms->search.s, ms->search.rm_len);
  2072. + cp = strndup(RCAST(const char *, ms->search.s),
  2073. + ms->search.rm_len);
  2074. if (cp == NULL) {
  2075. file_oomem(ms, ms->search.rm_len);
  2076. return -1;
  2077. @@ -805,7 +830,7 @@
  2078. file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
  2079. return -1;
  2080. }
  2081. - return (int32_t)t;
  2082. + return CAST(int32_t, t);
  2083. }
  2084. private int
  2085. @@ -852,7 +877,8 @@
  2086. p->s[strcspn(p->s, "\r\n")] = '\0';
  2087. o = CAST(uint32_t, (ms->offset + strlen(p->s)));
  2088. if (m->type == FILE_PSTRING)
  2089. - o += (uint32_t)file_pstring_length_size(m);
  2090. + o += CAST(uint32_t,
  2091. + file_pstring_length_size(m));
  2092. }
  2093. break;
  2094. @@ -918,7 +944,7 @@
  2095. case FILE_DER:
  2096. {
  2097. o = der_offs(ms, m, nbytes);
  2098. - if (o == -1 || (size_t)o > nbytes) {
  2099. + if (o == -1 || CAST(size_t, o) > nbytes) {
  2100. if ((ms->flags & MAGIC_DEBUG) != 0) {
  2101. (void)fprintf(stderr,
  2102. "Bad DER offset %d nbytes=%"
  2103. @@ -935,7 +961,7 @@
  2104. break;
  2105. }
  2106. - if ((size_t)o > nbytes) {
  2107. + if (CAST(size_t, o) > nbytes) {
  2108. #if 0
  2109. file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
  2110. "u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
  2111. @@ -1008,36 +1034,36 @@
  2112. return type;
  2113. }
  2114. }
  2115. -#define DO_CVT(fld, cast) \
  2116. +#define DO_CVT(fld, type) \
  2117. if (m->num_mask) \
  2118. switch (m->mask_op & FILE_OPS_MASK) { \
  2119. case FILE_OPAND: \
  2120. - p->fld &= cast m->num_mask; \
  2121. + p->fld &= CAST(type, m->num_mask); \
  2122. break; \
  2123. case FILE_OPOR: \
  2124. - p->fld |= cast m->num_mask; \
  2125. + p->fld |= CAST(type, m->num_mask); \
  2126. break; \
  2127. case FILE_OPXOR: \
  2128. - p->fld ^= cast m->num_mask; \
  2129. + p->fld ^= CAST(type, m->num_mask); \
  2130. break; \
  2131. case FILE_OPADD: \
  2132. - p->fld += cast m->num_mask; \
  2133. + p->fld += CAST(type, m->num_mask); \
  2134. break; \
  2135. case FILE_OPMINUS: \
  2136. - p->fld -= cast m->num_mask; \
  2137. + p->fld -= CAST(type, m->num_mask); \
  2138. break; \
  2139. case FILE_OPMULTIPLY: \
  2140. - p->fld *= cast m->num_mask; \
  2141. + p->fld *= CAST(type, m->num_mask); \
  2142. break; \
  2143. case FILE_OPDIVIDE: \
  2144. - if (cast m->num_mask == 0) \
  2145. + if (CAST(type, m->num_mask) == 0) \
  2146. return -1; \
  2147. - p->fld /= cast m->num_mask; \
  2148. + p->fld /= CAST(type, m->num_mask); \
  2149. break; \
  2150. case FILE_OPMODULO: \
  2151. - if (cast m->num_mask == 0) \
  2152. + if (CAST(type, m->num_mask) == 0) \
  2153. return -1; \
  2154. - p->fld %= cast m->num_mask; \
  2155. + p->fld %= CAST(type, m->num_mask); \
  2156. break; \
  2157. } \
  2158. if (m->mask_op & FILE_OPINVERSE) \
  2159. @@ -1046,61 +1072,61 @@
  2160. private int
  2161. cvt_8(union VALUETYPE *p, const struct magic *m)
  2162. {
  2163. - DO_CVT(b, (uint8_t));
  2164. + DO_CVT(b, uint8_t);
  2165. return 0;
  2166. }
  2167. private int
  2168. cvt_16(union VALUETYPE *p, const struct magic *m)
  2169. {
  2170. - DO_CVT(h, (uint16_t));
  2171. + DO_CVT(h, uint16_t);
  2172. return 0;
  2173. }
  2174. private int
  2175. cvt_32(union VALUETYPE *p, const struct magic *m)
  2176. {
  2177. - DO_CVT(l, (uint32_t));
  2178. + DO_CVT(l, uint32_t);
  2179. return 0;
  2180. }
  2181. private int
  2182. cvt_64(union VALUETYPE *p, const struct magic *m)
  2183. {
  2184. - DO_CVT(q, (uint64_t));
  2185. + DO_CVT(q, uint64_t);
  2186. return 0;
  2187. }
  2188. -#define DO_CVT2(fld, cast) \
  2189. +#define DO_CVT2(fld, type) \
  2190. if (m->num_mask) \
  2191. switch (m->mask_op & FILE_OPS_MASK) { \
  2192. case FILE_OPADD: \
  2193. - p->fld += cast m->num_mask; \
  2194. + p->fld += CAST(type, m->num_mask); \
  2195. break; \
  2196. case FILE_OPMINUS: \
  2197. - p->fld -= cast m->num_mask; \
  2198. + p->fld -= CAST(type, m->num_mask); \
  2199. break; \
  2200. case FILE_OPMULTIPLY: \
  2201. - p->fld *= cast m->num_mask; \
  2202. + p->fld *= CAST(type, m->num_mask); \
  2203. break; \
  2204. case FILE_OPDIVIDE: \
  2205. - if (cast m->num_mask == 0) \
  2206. + if (CAST(type, m->num_mask) == 0) \
  2207. return -1; \
  2208. - p->fld /= cast m->num_mask; \
  2209. + p->fld /= CAST(type, m->num_mask); \
  2210. break; \
  2211. } \
  2212. private int
  2213. cvt_float(union VALUETYPE *p, const struct magic *m)
  2214. {
  2215. - DO_CVT2(f, (float));
  2216. + DO_CVT2(f, float);
  2217. return 0;
  2218. }
  2219. private int
  2220. cvt_double(union VALUETYPE *p, const struct magic *m)
  2221. {
  2222. - DO_CVT2(d, (double));
  2223. + DO_CVT2(d, double);
  2224. return 0;
  2225. }
  2226. @@ -1165,14 +1191,14 @@
  2227. return 1;
  2228. }
  2229. case FILE_BESHORT:
  2230. - p->h = (short)BE16(p);
  2231. + p->h = CAST(short, BE16(p));
  2232. if (cvt_16(p, m) == -1)
  2233. goto out;
  2234. return 1;
  2235. case FILE_BELONG:
  2236. case FILE_BEDATE:
  2237. case FILE_BELDATE:
  2238. - p->l = (int32_t)BE32(p);
  2239. + p->l = CAST(int32_t, BE32(p));
  2240. if (cvt_32(p, m) == -1)
  2241. goto out;
  2242. return 1;
  2243. @@ -1180,19 +1206,19 @@
  2244. case FILE_BEQDATE:
  2245. case FILE_BEQLDATE:
  2246. case FILE_BEQWDATE:
  2247. - p->q = (uint64_t)BE64(p);
  2248. + p->q = CAST(uint64_t, BE64(p));
  2249. if (cvt_64(p, m) == -1)
  2250. goto out;
  2251. return 1;
  2252. case FILE_LESHORT:
  2253. - p->h = (short)LE16(p);
  2254. + p->h = CAST(short, LE16(p));
  2255. if (cvt_16(p, m) == -1)
  2256. goto out;
  2257. return 1;
  2258. case FILE_LELONG:
  2259. case FILE_LEDATE:
  2260. case FILE_LELDATE:
  2261. - p->l = (int32_t)LE32(p);
  2262. + p->l = CAST(int32_t, LE32(p));
  2263. if (cvt_32(p, m) == -1)
  2264. goto out;
  2265. return 1;
  2266. @@ -1200,14 +1226,14 @@
  2267. case FILE_LEQDATE:
  2268. case FILE_LEQLDATE:
  2269. case FILE_LEQWDATE:
  2270. - p->q = (uint64_t)LE64(p);
  2271. + p->q = CAST(uint64_t, LE64(p));
  2272. if (cvt_64(p, m) == -1)
  2273. goto out;
  2274. return 1;
  2275. case FILE_MELONG:
  2276. case FILE_MEDATE:
  2277. case FILE_MELDATE:
  2278. - p->l = (int32_t)ME32(p);
  2279. + p->l = CAST(int32_t, ME32(p));
  2280. if (cvt_32(p, m) == -1)
  2281. goto out;
  2282. return 1;
  2283. @@ -1388,7 +1414,7 @@
  2284. * might even cause problems
  2285. */
  2286. if (nbytes < sizeof(*p))
  2287. - (void)memset(((char *)(void *)p) + nbytes, '\0',
  2288. + (void)memset(RCAST(char *, RCAST(void *, p)) + nbytes, '\0',
  2289. sizeof(*p) - nbytes);
  2290. return 0;
  2291. }
  2292. @@ -1429,7 +1455,7 @@
  2293. if (m->in_op & FILE_OPINVERSE)
  2294. offset = ~offset;
  2295. - return (uint32_t)offset;
  2296. + return CAST(uint32_t, offset);
  2297. }
  2298. private int
  2299. @@ -1454,10 +1480,10 @@
  2300. "u at level %u", o, cont_level);
  2301. return -1;
  2302. }
  2303. - if ((size_t)-m->offset > b->elen)
  2304. + if (CAST(size_t, -m->offset) > b->elen)
  2305. return -1;
  2306. buffer_init(bb, -1, b->ebuf, b->elen);
  2307. - ms->eoffset = ms->offset = (int32_t)(b->elen + m->offset);
  2308. + ms->eoffset = ms->offset = CAST(int32_t, b->elen + m->offset);
  2309. } else {
  2310. if (cont_level == 0) {
  2311. normal:
  2312. @@ -1508,8 +1534,8 @@
  2313. - if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
  2314. - (uint32_t)nbytes, m) == -1)
  2315. + if (mcopy(ms, p, m->type, m->flag & INDIR, s,
  2316. + CAST(uint32_t, offset + o), CAST(uint32_t, nbytes), m) == -1)
  2317. return -1;
  2318. if ((ms->flags & MAGIC_DEBUG) != 0) {
  2319. @@ -1518,7 +1544,8 @@
  2320. "u, il=%hu, nc=%hu)\n",
  2321. m->type, m->flag, offset, o, nbytes,
  2322. *indir_count, *name_count);
  2323. - mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
  2324. + mdebug(offset, RCAST(char *, RCAST(void *, p)),
  2325. + sizeof(union VALUETYPE));
  2326. #ifndef COMPILE_ONLY
  2327. file_mdump(m);
  2328. #endif
  2329. @@ -1529,7 +1556,7 @@
  2330. const int sgn = m->in_op & FILE_OPSIGNED;
  2331. if (m->in_op & FILE_OPINDIRECT) {
  2332. const union VALUETYPE *q = CAST(const union VALUETYPE *,
  2333. - ((const void *)(s + offset + off)));
  2334. + RCAST(const void *, s + offset + off));
  2335. switch (cvt_flip(m->in_type, flip)) {
  2336. case FILE_BYTE:
  2337. if (OFFSET_OOB(nbytes, offset + off, 1))
  2338. @@ -1616,7 +1643,7 @@
  2339. return 0;
  2340. lhs = BE32(p);
  2341. if (in_type == FILE_BEID3)
  2342. - lhs = cvt_id3(ms, (uint32_t)lhs);
  2343. + lhs = cvt_id3(ms, CAST(uint32_t, lhs));
  2344. offset = do_ops(m, SEXT(sgn,32,lhs), off);
  2345. break;
  2346. case FILE_LELONG:
  2347. @@ -1625,7 +1652,7 @@
  2348. return 0;
  2349. lhs = LE32(p);
  2350. if (in_type == FILE_LEID3)
  2351. - lhs = cvt_id3(ms, (uint32_t)lhs);
  2352. + lhs = cvt_id3(ms, CAST(uint32_t, lhs));
  2353. offset = do_ops(m, SEXT(sgn,32,lhs), off);
  2354. break;
  2355. case FILE_MELONG:
  2356. @@ -1668,7 +1695,7 @@
  2357. ms->offset = offset;
  2358. if ((ms->flags & MAGIC_DEBUG) != 0) {
  2359. - mdebug(offset, (char *)(void *)p,
  2360. + mdebug(offset, RCAST(char *, RCAST(void *, p)),
  2361. sizeof(union VALUETYPE));
  2362. #ifndef COMPILE_ONLY
  2363. file_mdump(m);
  2364. @@ -1819,8 +1846,8 @@
  2365. * the ctype functions will work correctly without extra
  2366. * casting.
  2367. */
  2368. - const unsigned char *a = (const unsigned char *)s1;
  2369. - const unsigned char *b = (const unsigned char *)s2;
  2370. + const unsigned char *a = RCAST(const unsigned char *, s1);
  2371. + const unsigned char *b = RCAST(const unsigned char *, s2);
  2372. const unsigned char *eb = b + len;
  2373. uint64_t v;
  2374. @@ -2015,13 +2042,15 @@
  2375. case FILE_STRING:
  2376. case FILE_PSTRING:
  2377. l = 0;
  2378. - v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  2379. + v = file_strncmp(m->value.s, p->s, CAST(size_t, m->vallen),
  2380. + m->str_flags);
  2381. break;
  2382. case FILE_BESTRING16:
  2383. case FILE_LESTRING16:
  2384. l = 0;
  2385. - v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  2386. + v = file_strncmp16(m->value.s, p->s, CAST(size_t, m->vallen),
  2387. + m->str_flags);
  2388. break;
  2389. case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
  2390. @@ -2063,7 +2092,7 @@
  2391. ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
  2392. if (rc) {
  2393. file_regerror(&rx, rc, ms);
  2394. - v = (uint64_t)-1;
  2395. + v = CAST(uint64_t, -1);
  2396. } else {
  2397. regmatch_t pmatch;
  2398. size_t slen = ms->search.s_len;
  2399. @@ -2084,15 +2113,15 @@
  2400. search = CCAST(char *, "");
  2401. copy = NULL;
  2402. }
  2403. - rc = file_regexec(&rx, (const char *)search,
  2404. + rc = file_regexec(&rx, RCAST(const char *, search),
  2405. 1, &pmatch, 0);
  2406. free(copy);
  2407. switch (rc) {
  2408. case 0:
  2409. - ms->search.s += (int)pmatch.rm_so;
  2410. - ms->search.offset += (size_t)pmatch.rm_so;
  2411. - ms->search.rm_len =
  2412. - (size_t)(pmatch.rm_eo - pmatch.rm_so);
  2413. + ms->search.s += CAST(int, pmatch.rm_so);
  2414. + ms->search.offset += CAST(size_t, pmatch.rm_so);
  2415. + ms->search.rm_len = CAST(size_t,
  2416. + pmatch.rm_eo - pmatch.rm_so);
  2417. v = 0;
  2418. break;
  2419. @@ -2102,12 +2131,12 @@
  2420. default:
  2421. file_regerror(&rx, rc, ms);
  2422. - v = (uint64_t)-1;
  2423. + v = CAST(uint64_t, -1);
  2424. break;
  2425. }
  2426. }
  2427. file_regfree(&rx);
  2428. - if (v == (uint64_t)-1)
  2429. + if (v == CAST(uint64_t, -1))
  2430. return -1;
  2431. break;
  2432. }
  2433. @@ -2136,7 +2165,7 @@
  2434. case 'x':
  2435. if ((ms->flags & MAGIC_DEBUG) != 0)
  2436. (void) fprintf(stderr, "%" INT64_T_FORMAT
  2437. - "u == *any* = 1\n", (unsigned long long)v);
  2438. + "u == *any* = 1\n", CAST(unsigned long long, v));
  2439. matched = 1;
  2440. break;
  2441. @@ -2144,16 +2173,18 @@
  2442. matched = v != l;
  2443. if ((ms->flags & MAGIC_DEBUG) != 0)
  2444. (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
  2445. - INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
  2446. - (unsigned long long)l, matched);
  2447. + INT64_T_FORMAT "u = %d\n",
  2448. + CAST(unsigned long long, v),
  2449. + CAST(unsigned long long, l), matched);
  2450. break;
  2451. case '=':
  2452. matched = v == l;
  2453. if ((ms->flags & MAGIC_DEBUG) != 0)
  2454. (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
  2455. - INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
  2456. - (unsigned long long)l, matched);
  2457. + INT64_T_FORMAT "u = %d\n",
  2458. + CAST(unsigned long long, v),
  2459. + CAST(unsigned long long, l), matched);
  2460. break;
  2461. case '>':
  2462. @@ -2162,15 +2193,16 @@
  2463. if ((ms->flags & MAGIC_DEBUG) != 0)
  2464. (void) fprintf(stderr, "%" INT64_T_FORMAT
  2465. "u > %" INT64_T_FORMAT "u = %d\n",
  2466. - (unsigned long long)v,
  2467. - (unsigned long long)l, matched);
  2468. + CAST(unsigned long long, v),
  2469. + CAST(unsigned long long, l), matched);
  2470. }
  2471. else {
  2472. - matched = (int64_t) v > (int64_t) l;
  2473. + matched = CAST(int64_t, v) > CAST(int64_t, l);
  2474. if ((ms->flags & MAGIC_DEBUG) != 0)
  2475. (void) fprintf(stderr, "%" INT64_T_FORMAT
  2476. "d > %" INT64_T_FORMAT "d = %d\n",
  2477. - (long long)v, (long long)l, matched);
  2478. + CAST(long long, v),
  2479. + CAST(long long, l), matched);
  2480. }
  2481. break;
  2482. @@ -2180,15 +2212,16 @@
  2483. if ((ms->flags & MAGIC_DEBUG) != 0)
  2484. (void) fprintf(stderr, "%" INT64_T_FORMAT
  2485. "u < %" INT64_T_FORMAT "u = %d\n",
  2486. - (unsigned long long)v,
  2487. - (unsigned long long)l, matched);
  2488. + CAST(unsigned long long, v),
  2489. + CAST(unsigned long long, l), matched);
  2490. }
  2491. else {
  2492. - matched = (int64_t) v < (int64_t) l;
  2493. + matched = CAST(int64_t, v) < CAST(int64_t, l);
  2494. if ((ms->flags & MAGIC_DEBUG) != 0)
  2495. (void) fprintf(stderr, "%" INT64_T_FORMAT
  2496. "d < %" INT64_T_FORMAT "d = %d\n",
  2497. - (long long)v, (long long)l, matched);
  2498. + CAST(long long, v),
  2499. + CAST(long long, l), matched);
  2500. }
  2501. break;
  2502. @@ -2197,8 +2230,9 @@
  2503. if ((ms->flags & MAGIC_DEBUG) != 0)
  2504. (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
  2505. INT64_T_FORMAT "x) == %" INT64_T_FORMAT
  2506. - "x) = %d\n", (unsigned long long)v,
  2507. - (unsigned long long)l, (unsigned long long)l,
  2508. + "x) = %d\n", CAST(unsigned long long, v),
  2509. + CAST(unsigned long long, l),
  2510. + CAST(unsigned long long, l),
  2511. matched);
  2512. break;
  2513. @@ -2207,9 +2241,9 @@
  2514. if ((ms->flags & MAGIC_DEBUG) != 0)
  2515. (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
  2516. INT64_T_FORMAT "x) != %" INT64_T_FORMAT
  2517. - "x) = %d\n", (unsigned long long)v,
  2518. - (unsigned long long)l, (unsigned long long)l,
  2519. - matched);
  2520. + "x) = %d\n", CAST(unsigned long long, v),
  2521. + CAST(unsigned long long, l),
  2522. + CAST(unsigned long long, l), matched);
  2523. break;
  2524. default: