cherry-pick.FILE5_36-32-g479e0995.pr-69-only-consider-arrays-1-element-when-quickly-figuring-out-if-a.patch 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Subject: PR/69: Only consider arrays > 1 element when quickly figuring out if a (...)
  2. Origin: FILE5_36-32-g479e0995 <https://github.com/file/file/commit/FILE5_36-32-g479e0995>
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Sat Mar 2 01:08:10 2019 +0000
  5. Bug-Debian: https://bugs.debian.org/922874
  6. PR/69: Only consider arrays > 1 element when quickly figuring out if a
  7. file is JSON.
  8. --- a/src/is_json.c
  9. +++ b/src/is_json.c
  10. @@ -52,7 +52,8 @@
  11. #define JSON_NUMBER 2
  12. #define JSON_OBJECT 3
  13. #define JSON_STRING 4
  14. -#define JSON_MAX 5
  15. +#define JSON_ARRAYN 5
  16. +#define JSON_MAX 6
  17. /*
  18. * if JSON_COUNT != 0:
  19. @@ -171,6 +172,7 @@
  20. size_t *st, size_t lvl)
  21. {
  22. const unsigned char *uc = *ucp;
  23. + int more = 0; /* Array has more than 1 element */
  24. DPRINTF("Parse array: ", uc, *ucp);
  25. while (uc < ue) {
  26. @@ -180,9 +182,12 @@
  27. goto out;
  28. switch (*uc) {
  29. case ',':
  30. + more++;
  31. uc++;
  32. continue;
  33. case ']':
  34. + if (more)
  35. + st[JSON_ARRAYN]++;
  36. *ucp = uc + 1;
  37. return 1;
  38. default:
  39. @@ -330,7 +335,7 @@
  40. return 0;
  41. #if JSON_COUNT
  42. /* bail quickly if not counting */
  43. - if (lvl > 1 && (st[JSON_OBJECT] || st[JSON_ARRAY]))
  44. + if (lvl > 1 && (st[JSON_OBJECT] || st[JSON_ARRAYN]))
  45. return 1;
  46. #endif
  47. @@ -373,7 +378,7 @@
  48. *ucp = uc;
  49. DPRINTF("End general: ", uc, *ucp);
  50. if (lvl == 0)
  51. - return rv && (st[JSON_ARRAY] || st[JSON_OBJECT]);
  52. + return rv && (st[JSON_ARRAYN] || st[JSON_OBJECT]);
  53. return rv;
  54. }
  55. @@ -408,8 +413,10 @@
  56. #define P(n) st[n], st[n] > 1 ? "s" : ""
  57. if (file_printf(ms, " (%" SIZE_T_FORMAT "u object%s, %" SIZE_T_FORMAT
  58. "u array%s, %" SIZE_T_FORMAT "u string%s, %" SIZE_T_FORMAT
  59. - "u constant%s, %" SIZE_T_FORMAT "u number%s)", P(JSON_OBJECT),
  60. - P(JSON_ARRAY), P(JSON_STRING), P(JSON_CONSTANT), P(JSON_NUMBER))
  61. + "u constant%s, %" SIZE_T_FORMAT "u number%s, %" SIZE_T_FORMAT
  62. + "u >1array%s)",
  63. + P(JSON_OBJECT), P(JSON_ARRAY), P(JSON_STRING), P(JSON_CONSTANT),
  64. + P(JSON_NUMBER), P(JSON_ARRAYN))
  65. == -1)
  66. return -1;
  67. #endif