pbwire 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,12 +32,18 @@
32
32
 
33
33
  /* ---------------------------------------------------------- json names */
34
34
 
35
- /* protoc's ToJsonName: drop every '_', uppercase the letter after it. */
35
+ /* Shared camelizer behind j_json_name/j_camel_name: drop every '_',
36
+ * uppercase the letter after it, and lowercase the first letter when
37
+ * lower_first is set (protoc's ToCamelCase; without it, ToJsonName).
38
+ * Returns the full (untruncated) length; n >= cap means `out` holds a
39
+ * truncated prefix. Callers must treat that as overflow (explicit error,
40
+ * like j_copy_any_name's 0), never as a usable name: printing a prefix
41
+ * while parsing rejects it would silently diverge. */
36
42
  static size_t
37
- j_json_name(const char *name, char *out, size_t cap)
43
+ json_camel_impl(const char *name, char *out, size_t cap, int lower_first)
38
44
  {
39
45
  size_t n = 0;
40
- int upper = 0;
46
+ int upper = lower_first ? 1 : 0;
41
47
 
42
48
  for (; *name != '\0'; name++) {
43
49
  char c;
@@ -48,6 +54,9 @@ j_json_name(const char *name, char *out, size_t cap)
48
54
  }
49
55
  c = upper ? (char)toupper((unsigned char)*name) : *name;
50
56
  upper = 0;
57
+ if (n == 0 && lower_first) {
58
+ c = (char)tolower((unsigned char)c);
59
+ }
51
60
  if (n + 1 < cap) {
52
61
  out[n] = c;
53
62
  }
@@ -59,85 +68,393 @@ j_json_name(const char *name, char *out, size_t cap)
59
68
  return n;
60
69
  }
61
70
 
62
- /* protoc's ToCamelCase with a lowercased first letter. */
71
+ /* protoc's ToJsonName. Same overflow contract as above: n >= cap means
72
+ * truncated, caller must skip the candidate instead of comparing its
73
+ * prefix (which could alias two different field names). */
74
+ static size_t
75
+ j_json_name(const char *name, char *out, size_t cap)
76
+ {
77
+ return json_camel_impl(name, out, cap, 0);
78
+ }
79
+
80
+ /* protoc's ToCamelCase with a lowercased first letter. Same overflow
81
+ * contract as j_json_name. */
63
82
  static size_t
64
83
  j_camel_name(const char *name, char *out, size_t cap)
65
84
  {
66
- size_t n = 0;
67
- int upper = 1;
85
+ return json_camel_impl(name, out, cap, 1);
86
+ }
68
87
 
69
- for (; *name != '\0'; name++) {
70
- char c;
88
+ /* Field names print as their jsonName: protoc's ToCamelCase with a
89
+ * lowercased first letter is j_camel_name() below, used for parsing;
90
+ * printing keeps the mapping j_json_name() computes. Returns 1 when the
91
+ * name fits, 0 when truncated (explicit error, like j_copy_any_name and
92
+ * the extension branch of j_print_one). */
93
+ static int
94
+ j_print_name(const pbw_field *field, char *out, size_t cap)
95
+ {
96
+ return j_json_name(field->name, out, cap) < cap;
97
+ }
71
98
 
72
- if (*name == '_') {
73
- upper = 1;
99
+ /* The reference parser matches camelCase names, then proto names, then json
100
+ * names, in that order. The lookups below keep that order: the camel loop
101
+ * first, then the proto spelling via the O(1) name_map hash (like text.c's
102
+ * t_lookup_field), then the json loop. The hash must not go first: with
103
+ * hand-built fields ["fooBar"#1,"foo_bar"#2] the key "fooBar" is both
104
+ * field #1's proto name and field #2's camel name, and the reference
105
+ * resolves it to #2 (camel beats proto), so a proto-first hash would
106
+ * mis-hit #1. A key holding an embedded NUL matches nothing: the probe is
107
+ * NUL-terminated for the hash, so without this guard "a\0evil" would
108
+ * resolve as the prefix "a" (the extension branch and j_copy_any_name
109
+ * already guard the same way). A key that cannot fit the NUL-terminated
110
+ * probe (len >= PBW_NAME_BUF) cannot equal any loader-accepted name, and
111
+ * its camel/json image would only be shorter, so it misses outright instead
112
+ * of scanning truncated prefixes. */
113
+
114
+ /* One hash slot: FNV-1a of the spelling (like schema.c's name_hash), plus
115
+ * the field index + 1 (0 marks an empty slot). */
116
+ typedef struct {
117
+ uint32_t hash;
118
+ uint32_t row;
119
+ } j_slot;
120
+
121
+ /* Lazily built camel/json spelling indexes for one descriptor. Both tables
122
+ * are open-addressed at load <= 1/2, so a miss probes ~1 slot: without this
123
+ * every unknown key paid a full scan of strlen + camelize per field, which
124
+ * is quadratic in the field count for an object full of misses. The tables
125
+ * hold only non-extension spellings that fit PBW_NAME_BUF (truncated ones
126
+ * are uncomparable and skipped, as in the linear loops below), and a
127
+ * confirm recompute on hash hit keeps collisions and duplicate spellings
128
+ * resolving to the same first field the linear scan would find. */
129
+ typedef struct {
130
+ const pbw_descriptor *desc;
131
+ size_t count; /* desc->field_count at build; a mismatch stays linear */
132
+ size_t camel_size; /* power of two, 0 when unbuilt */
133
+ j_slot *camel;
134
+ size_t json_size;
135
+ j_slot *json;
136
+ } j_index;
137
+
138
+ /* Per-parse index cache: descriptors are frozen once linked, so an index
139
+ * built for one key stays valid for the rest of the parse. The Any
140
+ * re-walk shares the outer parse's cache instead of owning one. The
141
+ * spelling scratch lives here (not on the C stack of the per-level lookup
142
+ * functions): 16 KB of probe buffers per nesting level overflowed a 2 MB
143
+ * worker stack at PBW_MAX_DEPTH under ASan, while one root-owned buffer
144
+ * costs nothing per level. It is only ever used transiently (no lookup
145
+ * holds it across a recursive call), so sharing is safe. */
146
+ typedef struct {
147
+ j_index *v;
148
+ size_t len;
149
+ size_t cap;
150
+ /* Room for the longest spelling or "[scope.name]" key the descriptor
151
+ * loader accepts. */
152
+ char scratch[PBW_EXT_KEY_BUF];
153
+ } j_cache;
154
+
155
+ /* FNV-1a over raw bytes (keys may hold embedded NULs, which never match). */
156
+ static uint32_t
157
+ j_hash_bytes(const char *s, size_t len)
158
+ {
159
+ uint32_t h = 2166136261u;
160
+ size_t i;
161
+
162
+ for (i = 0; i < len; i++) {
163
+ h ^= (uint8_t)s[i];
164
+ h *= 16777619u;
165
+ }
166
+ return h;
167
+ }
168
+
169
+ /* Smallest power-of-two table holding `count` rows at load <= 1/2
170
+ * (minimum 8 slots); 0 when the size itself is unrepresentable. */
171
+ static size_t
172
+ j_index_size(size_t count)
173
+ {
174
+ size_t need;
175
+ size_t size = 8;
176
+
177
+ if (count > (SIZE_MAX - 8) / 2) {
178
+ return 0;
179
+ }
180
+ need = count * 2 + 8;
181
+ while (size < need) {
182
+ if (size > SIZE_MAX / 2) {
183
+ return 0;
184
+ }
185
+ size *= 2;
186
+ }
187
+ return size;
188
+ }
189
+
190
+ /* Fills an empty index from the descriptor; 0 on allocation failure, in
191
+ * which case the caller falls back to the linear loops (so a NOMEM here
192
+ * never fails the parse, it only skips the fast path). */
193
+ static int
194
+ j_index_build(j_index *ix, const pbw_descriptor *desc)
195
+ {
196
+ char tmp[PBW_NAME_BUF];
197
+ size_t size;
198
+ size_t i;
199
+
200
+ size = j_index_size(desc->field_count);
201
+ if (size == 0) {
202
+ return 0;
203
+ }
204
+ ix->camel = (j_slot *)calloc(size, sizeof(*ix->camel));
205
+ if (ix->camel == NULL) {
206
+ return 0;
207
+ }
208
+ ix->json = (j_slot *)calloc(size, sizeof(*ix->json));
209
+ if (ix->json == NULL) {
210
+ free(ix->camel);
211
+ ix->camel = NULL;
212
+ return 0;
213
+ }
214
+ ix->camel_size = size;
215
+ ix->json_size = size;
216
+ for (i = 0; i < desc->field_count; i++) {
217
+ const pbw_field *f = &desc->fields[i];
218
+ size_t m;
219
+ uint32_t h;
220
+ size_t slot;
221
+
222
+ if (f->is_extension) {
74
223
  continue;
75
224
  }
76
- c = upper ? (char)toupper((unsigned char)*name) : *name;
77
- upper = 0;
78
- if (n == 0) {
79
- c = (char)tolower((unsigned char)c);
225
+ m = j_camel_name(f->name, tmp, sizeof(tmp));
226
+ if (m >= sizeof(tmp)) {
227
+ continue;
80
228
  }
81
- if (n + 1 < cap) {
82
- out[n] = c;
229
+ h = j_hash_bytes(tmp, m);
230
+ slot = (size_t)(h & (uint32_t)(size - 1));
231
+ while (ix->camel[slot].row != 0) {
232
+ slot = (slot + 1) & (size - 1);
83
233
  }
84
- n++;
234
+ ix->camel[slot].hash = h;
235
+ ix->camel[slot].row = (uint32_t)(i + 1);
236
+ m = j_json_name(f->name, tmp, sizeof(tmp));
237
+ if (m >= sizeof(tmp)) {
238
+ continue;
239
+ }
240
+ h = j_hash_bytes(tmp, m);
241
+ slot = (size_t)(h & (uint32_t)(size - 1));
242
+ while (ix->json[slot].row != 0) {
243
+ slot = (slot + 1) & (size - 1);
244
+ }
245
+ ix->json[slot].hash = h;
246
+ ix->json[slot].row = (uint32_t)(i + 1);
85
247
  }
86
- if (cap > 0) {
87
- out[n < cap ? n : cap - 1] = '\0';
248
+ ix->desc = desc;
249
+ ix->count = desc->field_count;
250
+ return 1;
251
+ }
252
+
253
+ /* Exact lookup in one spelling table: hash-gated, with a camelize
254
+ * recompute confirming the hit, so collisions cannot mis-resolve. `tmp`
255
+ * is the parse-root scratch (PBW_EXT_KEY_BUF bytes), not a stack buffer:
256
+ * this runs once per nesting level. */
257
+ static const pbw_field *
258
+ j_index_lookup(const pbw_descriptor *desc, const j_slot *tab, size_t size,
259
+ uint32_t h, const char *key, size_t len, int use_json,
260
+ char *tmp)
261
+ {
262
+ size_t slot;
263
+
264
+ if (tab == NULL || size == 0) {
265
+ return NULL;
266
+ }
267
+ slot = (size_t)(h & (uint32_t)(size - 1));
268
+ for (;;) {
269
+ const pbw_field *f;
270
+ size_t m;
271
+
272
+ if (tab[slot].row == 0) {
273
+ return NULL;
274
+ }
275
+ if (tab[slot].hash == h) {
276
+ f = &desc->fields[tab[slot].row - 1];
277
+ if (!f->is_extension) {
278
+ /* The accepted spelling length stays PBW_NAME_BUF (like
279
+ * the linear loops below): the scratch only grew to
280
+ * share the buffer with the extension-key probe. */
281
+ m = use_json ? j_json_name(f->name, tmp, PBW_NAME_BUF)
282
+ : j_camel_name(f->name, tmp, PBW_NAME_BUF);
283
+ /* Truncated spellings never match: m >= PBW_NAME_BUF
284
+ * implies m != len (len < PBW_NAME_BUF was checked by
285
+ * the caller). */
286
+ if (m == len && memcmp(tmp, key, len) == 0) {
287
+ return f;
288
+ }
289
+ }
290
+ }
291
+ slot = (slot + 1) & (size - 1);
88
292
  }
89
- return n;
90
293
  }
91
294
 
92
- /* Field names print as their jsonName: protoc's ToCamelCase with a
93
- * lowercased first letter is j_camel_name() below, used for parsing;
94
- * printing keeps the mapping j_json_name() computes. */
295
+ /* Finds (building on first miss) the cached index for `desc`; NULL when
296
+ * the tables could not be built or the layout moved mid-parse, in which
297
+ * case the caller uses the linear loops. Never fails the parse. */
298
+ static const j_index *
299
+ j_cache_get(j_cache *cache, const pbw_descriptor *desc)
300
+ {
301
+ size_t i;
302
+
303
+ for (i = 0; i < cache->len; i++) {
304
+ if (cache->v[i].desc == desc) {
305
+ if (cache->v[i].count == desc->field_count) {
306
+ return &cache->v[i];
307
+ }
308
+ return NULL;
309
+ }
310
+ }
311
+ if (cache->len == cache->cap) {
312
+ size_t ncap = cache->cap == 0 ? 4 : cache->cap * 2;
313
+ j_index *nv;
314
+
315
+ if (cache->cap != 0 && ncap / 2 != cache->cap) {
316
+ return NULL;
317
+ }
318
+ nv = (j_index *)realloc(cache->v, ncap * sizeof(*nv));
319
+ if (nv == NULL) {
320
+ return NULL;
321
+ }
322
+ cache->v = nv;
323
+ cache->cap = ncap;
324
+ }
325
+ {
326
+ j_index ix;
327
+
328
+ memset(&ix, 0, sizeof(ix));
329
+ if (!j_index_build(&ix, desc)) {
330
+ return NULL;
331
+ }
332
+ cache->v[cache->len] = ix;
333
+ return &cache->v[cache->len++];
334
+ }
335
+ }
336
+
337
+ /* Releases every table built during the parse. */
95
338
  static void
96
- j_print_name(const pbw_field *field, char *out, size_t cap)
339
+ j_cache_free(j_cache *cache)
97
340
  {
98
- j_json_name(field->name, out, cap);
341
+ size_t i;
342
+
343
+ if (cache == NULL) {
344
+ return;
345
+ }
346
+ for (i = 0; i < cache->len; i++) {
347
+ free(cache->v[i].camel);
348
+ free(cache->v[i].json);
349
+ }
350
+ free(cache->v);
351
+ cache->v = NULL;
352
+ cache->len = 0;
353
+ cache->cap = 0;
99
354
  }
100
355
 
101
- /* The reference parser matches camelCase names, then proto names, then json
102
- * names, in that order. */
103
356
  static int
104
- j_field_by_name(const pbw_descriptor *desc, const char *key, size_t len,
105
- const pbw_field **out)
357
+ j_field_by_name(j_cache *cache, const pbw_descriptor *desc, const char *key,
358
+ size_t len, const pbw_field **out)
106
359
  {
107
- /* Room for the longest name pbw_descriptor_set_load() accepts (512
108
- * bytes); a shorter buffer would truncate and could alias two names. */
109
- char tmp[520];
360
+ /* The parse-root scratch: room for the longest name the descriptor
361
+ * loader accepts (PBW_NAME_BUF); a shorter buffer would truncate and
362
+ * could alias two names. A stack buffer here would cost 4 KB per
363
+ * nesting level (see j_cache). */
364
+ char *tmp;
365
+ const pbw_field *found;
110
366
  size_t i;
111
367
 
368
+ if (cache == NULL) {
369
+ return 0;
370
+ }
371
+ tmp = cache->scratch;
372
+ if (len >= PBW_NAME_BUF) {
373
+ return 0;
374
+ }
375
+ if (len > 0 && memchr(key, 0, len) != NULL) {
376
+ return 0;
377
+ }
378
+ {
379
+ uint32_t h = j_hash_bytes(key, len);
380
+ const j_index *ix = j_cache_get(cache, desc);
381
+
382
+ if (ix != NULL) {
383
+ found = j_index_lookup(desc, ix->camel, ix->camel_size, h, key,
384
+ len, 0, tmp);
385
+ if (found != NULL) {
386
+ *out = found;
387
+ return 1;
388
+ }
389
+ memcpy(tmp, key, len);
390
+ tmp[len] = '\0';
391
+ found = pbw_descriptor_find_field_name(desc, tmp);
392
+ /* Extensions are addressable only as "[full.name]"; a plain
393
+ * key must not resolve to one, or it would shadow a regular
394
+ * field or accept a spelling the JSON mapping does not
395
+ * define. */
396
+ if (found != NULL && !found->is_extension) {
397
+ *out = found;
398
+ return 1;
399
+ }
400
+ found = j_index_lookup(desc, ix->json, ix->json_size, h, key,
401
+ len, 1, tmp);
402
+ if (found != NULL) {
403
+ *out = found;
404
+ return 1;
405
+ }
406
+ return 0;
407
+ }
408
+ /* No index (allocation failure or a layout that moved mid-parse):
409
+ * fall through to the linear loops, which stay correct. */
410
+ }
112
411
  for (i = 0; i < desc->field_count; i++) {
113
412
  const pbw_field *f = &desc->fields[i];
114
413
  size_t n = strlen(f->name);
414
+ size_t m;
115
415
 
416
+ if (f->is_extension) {
417
+ continue;
418
+ }
116
419
  /* Camelizing drops underscores, so the JSON spelling can be shorter
117
420
  * than the proto name; only a name without one is length-checked,
118
421
  * which keeps the hot path at one compare. */
119
422
  if (n != len && strchr(f->name, '_') == NULL) {
120
423
  continue;
121
424
  }
122
- j_camel_name(f->name, tmp, sizeof(tmp));
123
- if (strlen(tmp) == len && memcmp(tmp, key, len) == 0) {
124
- *out = f;
125
- return 1;
425
+ m = j_camel_name(f->name, tmp, PBW_NAME_BUF);
426
+ /* Truncated: not comparable, skipping avoids aliasing two names
427
+ * that share a 4095-byte prefix (explicit-error style). */
428
+ if (m >= PBW_NAME_BUF) {
429
+ continue;
126
430
  }
127
- }
128
- for (i = 0; i < desc->field_count; i++) {
129
- const pbw_field *f = &desc->fields[i];
130
-
131
- if (strlen(f->name) == len && memcmp(f->name, key, len) == 0) {
431
+ if (m == len && memcmp(tmp, key, len) == 0) {
132
432
  *out = f;
133
433
  return 1;
134
434
  }
135
435
  }
436
+ memcpy(tmp, key, len);
437
+ tmp[len] = '\0';
438
+ found = pbw_descriptor_find_field_name(desc, tmp);
439
+ /* Extensions are addressable only as "[full.name]"; a plain key must
440
+ * not resolve to one, or it would shadow a regular field or accept a
441
+ * spelling the JSON mapping does not define. */
442
+ if (found != NULL && !found->is_extension) {
443
+ *out = found;
444
+ return 1;
445
+ }
136
446
  for (i = 0; i < desc->field_count; i++) {
137
447
  const pbw_field *f = &desc->fields[i];
448
+ size_t m;
138
449
 
139
- j_json_name(f->name, tmp, sizeof(tmp));
140
- if (strlen(tmp) == len && memcmp(tmp, key, len) == 0) {
450
+ if (f->is_extension) {
451
+ continue;
452
+ }
453
+ m = j_json_name(f->name, tmp, PBW_NAME_BUF);
454
+ if (m >= PBW_NAME_BUF) {
455
+ continue;
456
+ }
457
+ if (m == len && memcmp(tmp, key, len) == 0) {
141
458
  *out = f;
142
459
  return 1;
143
460
  }
@@ -201,12 +518,42 @@ j_classify(const pbw_descriptor *desc)
201
518
  return J_NOT_WKT;
202
519
  }
203
520
 
521
+ /* Copies the Any type name (after the last '/') into `out`, NUL-terminated.
522
+ * 1 on success; 0 when the name does not fit `out` or holds an embedded
523
+ * NUL (which would otherwise resolve as a truncated prefix). Shared by the
524
+ * print and parse paths so both accept the same PBW_NAME_BUF range. */
525
+ static int
526
+ j_copy_any_name(const uint8_t *url, size_t url_len, char *out, size_t cap)
527
+ {
528
+ size_t off = 0;
529
+ size_t i;
530
+ size_t n;
531
+
532
+ for (i = 0; i < url_len; i++) {
533
+ if (url[i] == '/') {
534
+ off = i + 1;
535
+ }
536
+ }
537
+ n = url_len - off;
538
+ if (n >= cap || memchr(url + off, 0, n) != NULL) {
539
+ return 0;
540
+ }
541
+ memcpy(out, url + off, n);
542
+ out[n] = '\0';
543
+ return 1;
544
+ }
545
+
204
546
  /* ---------------------------------------------------------------- output */
205
547
 
206
548
  typedef struct {
207
549
  pbw_buf buf;
208
550
  pbw_status status;
209
551
  uint32_t depth; /* message nesting, capped at PBW_MAX_DEPTH */
552
+ /* Name scratch (PBW_EXT_KEY_BUF bytes): j_print_one/_any run once per
553
+ * nesting level, so their key probes live here instead of on the C
554
+ * stack (same overflow as the parse path, see j_cache). Consumed
555
+ * before any recursive call, so sharing is safe. */
556
+ char scratch[PBW_EXT_KEY_BUF];
210
557
  } j_out;
211
558
 
212
559
  static void
@@ -1465,7 +1812,6 @@ j_print_any(j_out *j, const pbw_message *msg)
1465
1812
  size_t url_len = 0;
1466
1813
  const uint8_t *payload = NULL;
1467
1814
  size_t payload_len = 0;
1468
- const char *slash;
1469
1815
  const pbw_descriptor *resolved;
1470
1816
  pbw_message *decoded = NULL;
1471
1817
 
@@ -1490,29 +1836,16 @@ j_print_any(j_out *j, const pbw_message *msg)
1490
1836
  payload = bytes_v->as.bytes.data;
1491
1837
  payload_len = bytes_v->as.bytes.len;
1492
1838
  }
1493
- /* The type name is whatever follows the last '/'. */
1494
- slash = NULL;
1839
+ /* The type name is whatever follows the last '/'. The probe uses the
1840
+ * print-root scratch, not a stack buffer (see j_out). */
1495
1841
  {
1496
- size_t i;
1842
+ /* Room for the longest message name the loader accepts. */
1843
+ char *name = j->scratch;
1497
1844
 
1498
- for (i = 0; i < url_len; i++) {
1499
- if (url[i] == '/') {
1500
- slash = (const char *)url + i;
1501
- }
1502
- }
1503
- }
1504
- {
1505
- /* Room for the longest message name the loader accepts (512). */
1506
- char name[520];
1507
- size_t off = slash == NULL ? 0 : (size_t)(slash - (const char *)url) + 1;
1508
- size_t n = url_len - off;
1509
-
1510
- if (n >= sizeof(name)) {
1845
+ if (!j_copy_any_name(url, url_len, name, PBW_NAME_BUF)) {
1511
1846
  j->status = PBW_ERR_INVALID;
1512
1847
  return;
1513
1848
  }
1514
- memcpy(name, url + off, n);
1515
- name[n] = '\0';
1516
1849
  resolved = msg->desc->schema == NULL
1517
1850
  ? NULL
1518
1851
  : pbw_schema_find(msg->desc->schema, name);
@@ -1558,9 +1891,10 @@ static void
1558
1891
  j_print_one(j_out *j, const pbw_message *msg, const pbw_field *field,
1559
1892
  int *first)
1560
1893
  {
1561
- /* "[scope.name]" of an extension: the loader caps scope and name at 512
1562
- * bytes each, plus the brackets. */
1563
- char key[2 * 512 + 4];
1894
+ /* "[scope.name]" of an extension: scope + '.' + name plus brackets.
1895
+ * The print-root scratch, not a stack buffer (see j_out); consumed
1896
+ * by j_quoted before any value recursion. */
1897
+ char *key = j->scratch;
1564
1898
  const pbw_value *value = &msg->values[(size_t)(field - msg->desc->fields)];
1565
1899
 
1566
1900
  if (!*first) {
@@ -1571,7 +1905,7 @@ j_print_one(j_out *j, const pbw_message *msg, const pbw_field *field,
1571
1905
  const char *full =
1572
1906
  field->full_name != NULL ? field->full_name : field->name;
1573
1907
 
1574
- if (strlen(full) + 3 > sizeof(key)) {
1908
+ if (strlen(full) + 3 > PBW_EXT_KEY_BUF) {
1575
1909
  j->status = PBW_ERR_INVALID;
1576
1910
  return;
1577
1911
  }
@@ -1581,7 +1915,13 @@ j_print_one(j_out *j, const pbw_message *msg, const pbw_field *field,
1581
1915
  key[strlen(full) + 2] = '\0';
1582
1916
  j_quoted(j, (const uint8_t *)key, strlen(key));
1583
1917
  } else {
1584
- j_print_name(field, key, sizeof(key));
1918
+ /* A truncated display name is an explicit print error (like the
1919
+ * extension branch above and j_copy_any_name), not a silent
1920
+ * prefix: the parser would reject the overlong spelling. */
1921
+ if (!j_print_name(field, key, PBW_EXT_KEY_BUF)) {
1922
+ j->status = PBW_ERR_INVALID;
1923
+ return;
1924
+ }
1585
1925
  j_quoted(j, (const uint8_t *)key, strlen(key));
1586
1926
  }
1587
1927
  j_ch(j, ':');
@@ -1798,7 +2138,8 @@ typedef struct {
1798
2138
  j_cur cur;
1799
2139
  int ignore_unknown;
1800
2140
  uint32_t depth;
1801
- uint32_t dlim; /* nesting cap for this call (pbw_limits.max_depth) */
2141
+ uint32_t dlim; /* nesting cap for this call (pbw_limits.max_depth) */
2142
+ j_cache *cache; /* camel/json spelling indexes, owned by the parse root */
1802
2143
  } j_in;
1803
2144
 
1804
2145
  typedef enum {
@@ -2213,6 +2554,9 @@ j_atod(const char *text, size_t len, double *out)
2213
2554
  if (text == NULL || out == NULL) {
2214
2555
  return PBW_ERR_INVALID;
2215
2556
  }
2557
+ if (len > 0 && memchr(text, 0, len) != NULL) {
2558
+ return PBW_ERR_INVALID;
2559
+ }
2216
2560
  /* strtod skips leading whitespace itself, but not trailing garbage. A
2217
2561
  * number longer than the stack buffer is unusual, not invalid: the
2218
2562
  * reference parser has no length limit either. */
@@ -2227,9 +2571,6 @@ j_atod(const char *text, size_t len, double *out)
2227
2571
  }
2228
2572
  memcpy(tmp, text, len);
2229
2573
  tmp[len] = '\0';
2230
- if (tmp == NULL) {
2231
- return PBW_ERR_INVALID;
2232
- }
2233
2574
  v = pbw_strtod_c(tmp, &stop);
2234
2575
  if (stop == tmp) {
2235
2576
  goto invalid;
@@ -2264,7 +2605,7 @@ j_atob(const char *text, size_t len, int *out)
2264
2605
  char lower[8];
2265
2606
  size_t i;
2266
2607
 
2267
- if (len >= sizeof(lower)) {
2608
+ if (len >= sizeof(lower) || (len > 0 && memchr(text, 0, len) != NULL)) {
2268
2609
  return PBW_ERR_INVALID;
2269
2610
  }
2270
2611
  for (i = 0; i < len; i++) {
@@ -2804,9 +3145,9 @@ j_parse_scalar_value(j_in *j, pbw_message *msg, const pbw_field *field)
2804
3145
  pbw_buf raw;
2805
3146
 
2806
3147
  pbw_buf_init(&raw);
2807
- if (j_parse_string(j, &raw) != PBW_OK) {
3148
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
2808
3149
  pbw_buf_free(&raw);
2809
- return PBW_ERR_INVALID;
3150
+ return st;
2810
3151
  }
2811
3152
  st = j_parse_double_value(j, (const char *)raw.data, raw.len,
2812
3153
  field->type == PBW_TYPE_FLOAT, &v);
@@ -2865,9 +3206,9 @@ j_parse_scalar_value(j_in *j, pbw_message *msg, const pbw_field *field)
2865
3206
  pbw_buf raw;
2866
3207
 
2867
3208
  pbw_buf_init(&raw);
2868
- if (j_parse_string(j, &raw) != PBW_OK) {
3209
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
2869
3210
  pbw_buf_free(&raw);
2870
- return PBW_ERR_INVALID;
3211
+ return st;
2871
3212
  }
2872
3213
  st = j_parse_int_value(j, (const char *)raw.data, raw.len, 1,
2873
3214
  is_signed, bits32, &bits);
@@ -2898,9 +3239,9 @@ j_parse_scalar_value(j_in *j, pbw_message *msg, const pbw_field *field)
2898
3239
  pbw_status st;
2899
3240
 
2900
3241
  pbw_buf_init(&raw);
2901
- if (j_parse_string(j, &raw) != PBW_OK) {
3242
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
2902
3243
  pbw_buf_free(&raw);
2903
- return PBW_ERR_INVALID;
3244
+ return st;
2904
3245
  }
2905
3246
  st = j_atob((const char *)raw.data, raw.len, &flag);
2906
3247
  pbw_buf_free(&raw);
@@ -2921,9 +3262,9 @@ j_parse_scalar_value(j_in *j, pbw_message *msg, const pbw_field *field)
2921
3262
  return PBW_ERR_INVALID;
2922
3263
  }
2923
3264
  pbw_buf_init(&raw);
2924
- if (j_parse_string(j, &raw) != PBW_OK) {
3265
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
2925
3266
  pbw_buf_free(&raw);
2926
- return PBW_ERR_INVALID;
3267
+ return st;
2927
3268
  }
2928
3269
  if (field->type == PBW_TYPE_STRING) {
2929
3270
  st = j_store_bytes(j, msg, field, raw.data, raw.len);
@@ -2957,6 +3298,7 @@ j_parse_enum_value(j_in *j, pbw_message *msg, const pbw_field *field,
2957
3298
  {
2958
3299
  j_kind kind = j_peek_kind(j);
2959
3300
  int32_t number = 0;
3301
+ pbw_status est;
2960
3302
 
2961
3303
  *stored = 1;
2962
3304
  if (kind == JK_STR) {
@@ -2965,15 +3307,18 @@ j_parse_enum_value(j_in *j, pbw_message *msg, const pbw_field *field,
2965
3307
  size_t len;
2966
3308
 
2967
3309
  pbw_buf_init(&raw);
2968
- if (j_parse_string(j, &raw) != PBW_OK ||
2969
- !pbw_buf_reserve(&raw, 1)) {
3310
+ if ((est = j_parse_string(j, &raw)) != PBW_OK) {
2970
3311
  pbw_buf_free(&raw);
2971
- return PBW_ERR_INVALID;
3312
+ return est;
3313
+ }
3314
+ if (!pbw_buf_reserve(&raw, 1)) {
3315
+ pbw_buf_free(&raw);
3316
+ return PBW_ERR_NOMEM;
2972
3317
  }
2973
3318
  raw.data[raw.len] = '\0';
2974
3319
  text = (const char *)raw.data;
2975
3320
  len = raw.len;
2976
- if (field->sub != NULL &&
3321
+ if (field->sub != NULL && memchr(text, 0, len) == NULL &&
2977
3322
  pbw_enum_find_value(field->sub, text, &number)) {
2978
3323
  pbw_buf_free(&raw);
2979
3324
  } else {
@@ -3077,6 +3422,7 @@ j_parse_timestamp(j_in *j, pbw_message *msg)
3077
3422
  int64_t epoch_days;
3078
3423
  uint32_t parts[6];
3079
3424
  const char *suffix[6] = {"-", "-", "T", ":", ":", ""};
3425
+ pbw_status st;
3080
3426
 
3081
3427
  secs_field = j_time_field(msg->desc, 1, PBW_TYPE_INT64);
3082
3428
  nanos_field = j_time_field(msg->desc, 2, PBW_TYPE_INT32);
@@ -3087,9 +3433,9 @@ j_parse_timestamp(j_in *j, pbw_message *msg)
3087
3433
  return j_expect_lit(j, "null");
3088
3434
  }
3089
3435
  pbw_buf_init(&raw);
3090
- if (j_parse_string(j, &raw) != PBW_OK) {
3436
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
3091
3437
  pbw_buf_free(&raw);
3092
- return PBW_ERR_INVALID;
3438
+ return st;
3093
3439
  }
3094
3440
  /* strptime-free: fixed offsets, exactly like the reference. */
3095
3441
  data = (const char *)raw.data;
@@ -3296,6 +3642,7 @@ j_parse_duration(j_in *j, pbw_message *msg)
3296
3642
  const char *rest;
3297
3643
  size_t rest_len;
3298
3644
  size_t i;
3645
+ pbw_status st;
3299
3646
  const pbw_field *secs_field =
3300
3647
  j_time_field(msg->desc, 1, PBW_TYPE_INT64);
3301
3648
  const pbw_field *nanos_field =
@@ -3308,9 +3655,9 @@ j_parse_duration(j_in *j, pbw_message *msg)
3308
3655
  return j_expect_lit(j, "null");
3309
3656
  }
3310
3657
  pbw_buf_init(&raw);
3311
- if (j_parse_string(j, &raw) != PBW_OK) {
3658
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
3312
3659
  pbw_buf_free(&raw);
3313
- return PBW_ERR_INVALID;
3660
+ return st;
3314
3661
  }
3315
3662
  /* NUL-terminate for the integer scan. */
3316
3663
  if (!pbw_buf_reserve(&raw, 1)) {
@@ -3390,14 +3737,15 @@ j_parse_fieldmask(j_in *j, pbw_message *msg)
3390
3737
  const pbw_field *paths;
3391
3738
  const char *p;
3392
3739
  const char *end;
3740
+ pbw_status st;
3393
3741
 
3394
3742
  if (j_peek_kind(j) == JK_NULL) {
3395
3743
  return j_expect_lit(j, "null");
3396
3744
  }
3397
3745
  pbw_buf_init(&raw);
3398
- if (j_parse_string(j, &raw) != PBW_OK) {
3746
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
3399
3747
  pbw_buf_free(&raw);
3400
- return PBW_ERR_INVALID;
3748
+ return st;
3401
3749
  }
3402
3750
  paths = pbw_descriptor_find_field(msg->desc, 1);
3403
3751
  if (paths == NULL) {
@@ -3478,7 +3826,9 @@ j_parse_value_msg(j_in *j, pbw_message *msg)
3478
3826
  double d;
3479
3827
 
3480
3828
  if (j_parse_raw(j, &text, &len) != PBW_OK ||
3481
- j_atod(text, len, &d) != PBW_OK) {
3829
+ j_atod(text, len, &d) != PBW_OK || !isfinite(d)) {
3830
+ /* JSON has no spelling for a number past a double's range; the
3831
+ * plain double field path rejects the same token. */
3482
3832
  return PBW_ERR_INVALID;
3483
3833
  }
3484
3834
  {
@@ -3496,9 +3846,9 @@ j_parse_value_msg(j_in *j, pbw_message *msg)
3496
3846
  pbw_status st;
3497
3847
 
3498
3848
  pbw_buf_init(&raw);
3499
- if (j_parse_string(j, &raw) != PBW_OK) {
3849
+ if ((st = j_parse_string(j, &raw)) != PBW_OK) {
3500
3850
  pbw_buf_free(&raw);
3501
- return PBW_ERR_INVALID;
3851
+ return st;
3502
3852
  }
3503
3853
  st = j_store_bytes(j, msg, f3, raw.data, raw.len);
3504
3854
  pbw_buf_free(&raw);
@@ -3887,21 +4237,19 @@ j_parse_array(j_in *j, pbw_message *msg, const pbw_field *field)
3887
4237
  if (j_expect_ch(j, '[') != PBW_OK) {
3888
4238
  return PBW_ERR_INVALID;
3889
4239
  }
3890
- if (j->depth >= j->dlim) {
3891
- return PBW_ERR_LIMIT;
3892
- }
3893
- j->depth++;
4240
+ /* The array wrapper is not a message level (like the text parser's list
4241
+ * and the binary decoder): only the element messages count toward dlim.
4242
+ * Nested-array flattening in j_parse_element keeps its own depth check,
4243
+ * so unbounded '[' nesting still stops at the cap. */
3894
4244
  j_skip(&j->cur);
3895
4245
  if (j->cur.p < j->cur.end && *j->cur.p == ']') {
3896
4246
  j->cur.p++;
3897
- j->depth--;
3898
4247
  return PBW_OK;
3899
4248
  }
3900
4249
  for (;;) {
3901
4250
  pbw_status st = j_parse_element(j, msg, field);
3902
4251
 
3903
4252
  if (st != PBW_OK) {
3904
- j->depth--;
3905
4253
  return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
3906
4254
  }
3907
4255
  j_skip(&j->cur);
@@ -3911,16 +4259,13 @@ j_parse_array(j_in *j, pbw_message *msg, const pbw_field *field)
3911
4259
  j_skip(&j->cur);
3912
4260
  if (j->cur.p < j->cur.end && *j->cur.p == ']') {
3913
4261
  j->cur.p++;
3914
- j->depth--;
3915
4262
  return PBW_OK;
3916
4263
  }
3917
4264
  continue;
3918
4265
  }
3919
4266
  if (j_expect_ch(j, ']') != PBW_OK) {
3920
- j->depth--;
3921
4267
  return PBW_ERR_INVALID;
3922
4268
  }
3923
- j->depth--;
3924
4269
  return PBW_OK;
3925
4270
  }
3926
4271
  }
@@ -4213,11 +4558,11 @@ j_parse_any(j_in *j, pbw_message *msg)
4213
4558
  return PBW_ERR_INVALID;
4214
4559
  }
4215
4560
  pbw_buf_init(&raw);
4216
- if (j_parse_string(j, &raw) != PBW_OK) {
4561
+ if ((ust = j_parse_string(j, &raw)) != PBW_OK) {
4217
4562
  pbw_buf_free(&raw);
4218
4563
  pbw_buf_free(&key);
4219
4564
  j->depth = depth0;
4220
- return PBW_ERR_INVALID;
4565
+ return ust;
4221
4566
  }
4222
4567
  /* The stored type_url is the full URL; resolution strips it. */
4223
4568
  ust = j_store_bytes(j, msg, url_field, raw.data, raw.len);
@@ -4262,25 +4607,19 @@ j_parse_any(j_in *j, pbw_message *msg)
4262
4607
  return PBW_ERR_INVALID;
4263
4608
  }
4264
4609
  /* The type name is whatever follows the last '/'; the stored type_url
4265
- * keeps the full URL. */
4610
+ * keeps the full URL. The probe uses the parse-root scratch, not a
4611
+ * stack buffer: Any nests, so this runs once per nesting level. */
4266
4612
  {
4267
- const uint8_t *slash = NULL;
4268
- /* Room for the longest message name the loader accepts (512). */
4269
- char name[520];
4270
- size_t off;
4271
- size_t i;
4613
+ /* The stored type_url keeps the full URL; only the part after the
4614
+ * last '/' resolves against the schema. */
4615
+ char *name = (j->cache != NULL) ? j->cache->scratch : NULL;
4272
4616
 
4273
- for (i = 0; i < url_len; i++) {
4274
- if (url[i] == '/') {
4275
- slash = url + i;
4276
- }
4617
+ if (name == NULL) {
4618
+ return PBW_ERR_NOMEM;
4277
4619
  }
4278
- off = slash == NULL ? 0 : (size_t)(slash - url) + 1;
4279
- if (url_len - off >= sizeof(name)) {
4620
+ if (!j_copy_any_name(url, url_len, name, PBW_NAME_BUF)) {
4280
4621
  return PBW_ERR_INVALID;
4281
4622
  }
4282
- memcpy(name, url + off, url_len - off);
4283
- name[url_len - off] = '\0';
4284
4623
  resolved = msg->desc->schema == NULL
4285
4624
  ? NULL
4286
4625
  : pbw_schema_find(msg->desc->schema, name);
@@ -4306,6 +4645,7 @@ j_parse_any(j_in *j, pbw_message *msg)
4306
4645
  inner.ignore_unknown = j->ignore_unknown;
4307
4646
  inner.depth = j->depth + 1;
4308
4647
  inner.dlim = j->dlim;
4648
+ inner.cache = j->cache;
4309
4649
  /* start already points past the '{' the scan consumed. */
4310
4650
  for (;;) {
4311
4651
  pbw_buf key;
@@ -4396,16 +4736,30 @@ j_parse_named_field(j_in *j, pbw_message *msg, const uint8_t *key,
4396
4736
 
4397
4737
  if (len >= 2 && key[0] == '[' && key[len - 1] == ']') {
4398
4738
  /* Extension names are dotted and global, like the text format:
4399
- * room for scope + '.' + name, both capped at 512 bytes. */
4400
- char name[1028];
4739
+ * room for scope + '.' + name plus brackets. The probe uses the
4740
+ * parse-root scratch, not a stack buffer: this runs once per
4741
+ * nesting level (see j_cache). */
4742
+ char *name = (j->cache != NULL) ? j->cache->scratch : NULL;
4401
4743
  const pbw_field *ext;
4402
4744
 
4403
- if (len - 2 >= sizeof(name)) {
4745
+ if (name == NULL) {
4746
+ return PBW_ERR_NOMEM;
4747
+ }
4748
+ if (len - 2 >= PBW_EXT_KEY_BUF) {
4749
+ /* An overlong "[...]" key can never name an extension, but
4750
+ * like any other unknown key it is skipped under
4751
+ * ignore_unknown instead of failing (the miss branches below
4752
+ * do the same). */
4753
+ if (j->ignore_unknown) {
4754
+ return j_skip_value(j);
4755
+ }
4404
4756
  return PBW_ERR_INVALID;
4405
4757
  }
4406
4758
  memcpy(name, key + 1, len - 2);
4407
4759
  name[len - 2] = '\0';
4408
- ext = msg->desc->schema == NULL
4760
+ /* A name holding a NUL byte is no name: matching a prefix of it
4761
+ * would resolve a field the caller did not spell. */
4762
+ ext = msg->desc->schema == NULL || memchr(name, 0, len - 2) != NULL
4409
4763
  ? NULL
4410
4764
  : pbw_schema_find_extension(msg->desc->schema, name);
4411
4765
  if (ext == NULL) {
@@ -4419,7 +4773,8 @@ j_parse_named_field(j_in *j, pbw_message *msg, const uint8_t *key,
4419
4773
  return PBW_ERR_INVALID;
4420
4774
  }
4421
4775
  field = ext;
4422
- } else if (!j_field_by_name(msg->desc, (const char *)key, len, &field)) {
4776
+ } else if (!j_field_by_name(j->cache, msg->desc, (const char *)key, len,
4777
+ &field)) {
4423
4778
  if (j->ignore_unknown) {
4424
4779
  return j_skip_value(j);
4425
4780
  }
@@ -4716,6 +5071,7 @@ pbw_json_parse_limits(const pbw_descriptor *desc, const char *text,
4716
5071
  pbw_message **out)
4717
5072
  {
4718
5073
  j_in j;
5074
+ j_cache cache;
4719
5075
  pbw_message *msg;
4720
5076
  uint32_t dlim = PBW_MAX_DEPTH;
4721
5077
 
@@ -4755,24 +5111,32 @@ pbw_json_parse_limits(const pbw_descriptor *desc, const char *text,
4755
5111
  options != NULL && options->ignore_unknown_fields;
4756
5112
  j.depth = 0;
4757
5113
  j.dlim = dlim;
5114
+ j.cache = &cache;
5115
+ cache.v = NULL;
5116
+ cache.len = 0;
5117
+ cache.cap = 0;
4758
5118
  {
4759
5119
  pbw_status st = j_parse_message(&j, msg);
4760
5120
 
4761
5121
  if (st != PBW_OK) {
4762
5122
  pbw_message_free(msg);
5123
+ j_cache_free(&cache);
4763
5124
  return st;
4764
5125
  }
4765
5126
  }
4766
5127
  j_skip(&j.cur);
4767
5128
  if (j.cur.p != j.cur.end) {
4768
5129
  pbw_message_free(msg);
5130
+ j_cache_free(&cache);
4769
5131
  return PBW_ERR_INVALID;
4770
5132
  }
4771
5133
  /* A missing proto2 `required` field fails, like the binary decoder. */
4772
5134
  if (desc->has_required && !pbw_message_is_initialized(msg)) {
4773
5135
  pbw_message_free(msg);
5136
+ j_cache_free(&cache);
4774
5137
  return PBW_ERR_REQUIRED;
4775
5138
  }
5139
+ j_cache_free(&cache);
4776
5140
  *out = msg;
4777
5141
  return PBW_OK;
4778
5142
  }