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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8781f7d495650c1f5ce775c999f09e5a630254ce1f5b59ef0c0a51d221764124
4
- data.tar.gz: 371d44ed08a68b18d2bf4f42486a04b5433f9fb566ed44119261fe5e4e1e1af4
3
+ metadata.gz: b427d4674f3ea14374533e2df0af698e1a4ed99ba77d52fcbbcb5f5ad8efcc47
4
+ data.tar.gz: 55008531f0fcf601f924ed3a7e1747a5e01c52e388233890e5facfaba717eb6f
5
5
  SHA512:
6
- metadata.gz: 68811e3f5b8d69260f00999276eb1e56cd67863a20e10cce6c3232b56791ef7cce04a075ac1ab11e0a0cbc10ad970397ac8a2c36e873fd63fa3ba60bb17f4deb
7
- data.tar.gz: fa1022f7047cb609c6b2744abc1a58ec192f2fb03580cd2290ecbf9df05bce853d8eb5aa3fcc04e935b83df39db44a1eba8fa41f054ab31a19b343fd79abcd86
6
+ metadata.gz: 2a99a4e2f7b2494af06fb6eb02e5eb4013deec080c69c48eebdcd9f588837be3b306070d326e49dc480425bdf0d63b5928b314c4ae29fc89bc98ae2144a3647d
7
+ data.tar.gz: 9ac0d6c6c34fbf8320569c1e41d6a1a5a485049f6fd9e4fd62c6beb2b174a349589c8fdf1c1f41f6981a2d019d72901533b152577c10e7b9dc9c24f014205eb4
data/README.md CHANGED
@@ -69,6 +69,15 @@ make ruby-test # builds, then runs test/test_pbwire.rb
69
69
  ruby -Ibindings/ruby/lib bindings/ruby/examples/roundtrip.rb
70
70
  ```
71
71
 
72
+ The gem cannot pack symlinks or parent paths, so the C core travels as real
73
+ files under `ext/pbwire_ext/` (`core_*.c` + `pbwire.h` + `internal.h`). After
74
+ changing the core (`src/*.c`, `src/*.h`, `include/pbwire.h`), refresh the copy
75
+ from the repo root:
76
+
77
+ ```shell
78
+ make ruby-core-sync # one-key vendored-core refresh (see Makefile)
79
+ ```
80
+
72
81
  `RUBY=` selects the interpreter, both for the build and the benchmark:
73
82
 
74
83
  ```shell
@@ -201,7 +210,7 @@ the wire). Extensions are read by number, not name.
201
210
  wrap the C API; `to_builder` materializes a decoded view into a canonical
202
211
  tree. Unknown fields round-trip byte for byte in every form.
203
212
 
204
- ## Benchmark
213
+ ## Benchmarks
205
214
 
206
215
  `benchmarks/bench.sh` runs the four standard tiers from
207
216
  [`BENCHMARKS.md`](../../BENCHMARKS.md) against the `google-protobuf` gem on the
@@ -209,31 +218,70 @@ same `matrix3.proto` (small / medium / large / xlarge scale the repeated fields
209
218
  x0 / x1 / x100 / x3000). Both sides build the same message and must produce
210
219
  identical bytes before anything is timed; the benchmark
211
220
  interleaves the implementations and keeps the best of several runs. It needs
212
- `protoc`, a C compiler and the gem. Copied from that file, which is the single
213
- source for all performance numbers; higher is better, MB/s over wire bytes:
214
-
215
- | payload | op | pbwire-ruby (MB/s, x gem) | gem (MB/s) |
216
- |---|---|---|---|
217
- | 136 B | encode | 1015.0 (1.4x) | 741.1 |
218
- | 136 B | decode | 462.9 (1.8x) | 263.5 |
219
- | 174 B | encode | 956.9 (1.2x) | 803.2 |
220
- | 174 B | decode | 514.3 (1.9x) | 267.4 |
221
- | 2469 B | encode | 1473.1 (1.2x) | 1256.4 |
222
- | 2469 B | decode | 970.6 (1.8x) | 528.9 |
223
- | 74913 B | encode | 1582.4 (1.2x) | 1310.6 |
224
- | 74913 B | decode | 1170.0 (1.7x) | 694.7 |
221
+ `protoc`, a C compiler and the gem.
225
222
 
226
223
  ```shell
227
224
  gem install google-protobuf
228
225
  RUBY=$(command -v ruby) make ruby-bench
229
226
  ```
230
227
 
231
- The win is architectural, not a micro-optimization:
228
+ The per-field `tree` build is a `pbench/`-only arm (see [`pbench/README.md`](pbench/README.md) for the arms, gate and verdict); the published row is `shim` below.
232
229
 
233
- - **Encoding** writes one pass from the C tree straight into the String that is
234
- returned; there is no intermediate buffer and no per-field Ruby call.
235
- - **Decoding** is one validating scan that records where each wire field lives;
236
- the gem materializes every field.
230
+ The `shim` row is the native-object path from `pbench/`: one C-extension entry
231
+ walks a generated model's instance variables and fills the tree
232
+ (`encode_object`), or runs one layout-table tag scan straight into the generated
233
+ model classes (`decode_object`). It removes the Ruby-side per-field loop; it leads
234
+ on every tier on both ops (2.32x-5.03x encode, 5.55x-6.11x decode). See
235
+ [`pbench/README.md`](pbench/README.md) for the arms, gate and verdict.
236
+
237
+ | arm | mechanism | status |
238
+ |---|---|---|
239
+ | `tree` | prebuilt `Message` builder / wire view | `pbench/`-only arm (see `pbench/README.md`) |
240
+ | `shim` | one C-extension object walk over the generated model | shipped, `pbwire-ruby-shim` (published) |
241
+ | `core` | pure-Ruby port of the C core | `n/a`: `shortcut: no pure-Ruby core, reopen when a native-less Ruby target appears` |
242
+
243
+ #### Ruby (C extension) vs the `google-protobuf` gem
244
+
245
+ ![Ruby (C extension) vs the google-protobuf gem — MB/s by tier](../../docs/charts/bench-ruby-c-extension-vs-the-google-protobuf-gem.svg)
246
+
247
+ | payload | op | pbwire-ruby-shim (MB/s, x gem) | gem (MB/s) |
248
+ |---|---|---|---|
249
+ | 136 B | encode | 100.5 (2.32x ✅) | 43.4 |
250
+ | 136 B | decode | 126.1 (6.00x ✅) | 21.0 |
251
+ | 174 B | encode | 106.9 (3.47x ✅) | 30.8 |
252
+ | 174 B | decode | 139.4 (5.93x ✅) | 23.5 |
253
+ | 2469 B | encode | 89.0 (4.76x ✅) | 18.7 |
254
+ | 2469 B | decode | 153.1 (5.55x ✅) | 27.6 |
255
+ | 74913 B | encode | 100.6 (5.03x ✅) | 20.0 |
256
+ | 74913 B | decode | 184.6 (6.11x ✅) | 30.2 |
257
+
258
+ Ruby is a C extension over the same core, so this is C extension against C
259
+ extension. The first version drove the same C ABI through stdlib Fiddle and lost
260
+ every row by an order of magnitude: a downcall costs ~3 µs, so a 136 B encode was
261
+ 3.3 µs against the core's ~65 ns, and the tiers could only be compared by padding
262
+ `str` because the wrapper had no repeated fields. The rewrite removed the
263
+ boundary, added the repeated-field payload (so the xlarge tier is 3000 nested
264
+ messages, not one 74 KB string), and split the message into two representations.
265
+ The pbwire column is the native-in/native-out full task
266
+ (`pbench/`), formal run
267
+ `../../results/bench-ruby-20261124T174520Z.json`:
268
+
269
+ - **`shim` (native object in and out).** `type.encode_object(obj)` walks the
270
+ generated model inside C in one entry call and fills a pbw tree; `decode_object`
271
+ runs one layout-table tag scan straight into the generated model classes, with
272
+ no intermediate tree and no per-field API. It is the
273
+ published comparison on both ops (2.32x-5.03x encode / 5.55x-6.11x decode vs the gem) and the
274
+ simplest -- one extension call removes the Ruby per-field loop. Both sides
275
+ observe the full payload on decode: the shim entry materializes every repeated
276
+ element and nested model before it returns, while the gem arm folds the same
277
+ fields (every repeated element) through its accessors into a live sink -- a bare
278
+ gem parse on its own would time a shallower task. Encode leads
279
+ on every tier: both sides build the model fresh per iteration, and one C walk
280
+ from Ruby ivars beats the gem's object fill. The per-field `tree` arm is compared in
281
+ `pbench/README.md`, not here.
282
+
283
+ The old `tree` headline is retired: the published row is the native-object
284
+ `shim` arm.
237
285
 
238
286
  ## Conformance
239
287
 
@@ -281,3 +329,4 @@ tests need no `protoc`):
281
329
  constructors and accessors, `#encode(schema)` and `.decode(schema, wire)`. The
282
330
  committed output is `test/gen/` and `examples/gen/`; `examples/roundtrip.rb`
283
331
  uses typed assignments only. See `../../codegen/README.md`.
332
+
@@ -64,7 +64,7 @@ pbw_ctx_new_mem(void *mem, size_t len)
64
64
  ctx = (pbw_ctx *)mem;
65
65
  memset(ctx, 0, sizeof(*ctx));
66
66
  pbw_buf_init(&ctx->out);
67
- /* No heap here: pbw_encode_ctx() reports NOMEM instead of allocating; the
67
+ /* No heap here: pbw_encode_ctx() reports INVALID instead of allocating; the
68
68
  * encoder for this kind of context is pbw_encode_to(caller memory). */
69
69
  ctx->out.fixed = 1;
70
70
  if (!pbw_arena_init_mem(&ctx->arena, (uint8_t *)mem + hdr, len - hdr)) {
@@ -132,6 +132,8 @@ pbw_ctx_reset(pbw_ctx *ctx)
132
132
  size_t
133
133
  pbw_ctx_used(const pbw_ctx *ctx)
134
134
  {
135
+ /* Arena only: the reusable encode buffer (out.cap) is excluded, so size
136
+ * retain_cap budgets against used() plus the live out.cap. */
135
137
  return ctx == NULL ? 0 : pbw_arena_used(&ctx->arena);
136
138
  }
137
139
 
@@ -183,19 +185,19 @@ pbw_pool_acquire(pbw_pool *pool)
183
185
  return ctx;
184
186
  }
185
187
 
186
- void
188
+ pbw_status
187
189
  pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx)
188
190
  {
189
191
  pbw_ctx **slot;
190
192
 
191
193
  if (pool == NULL || ctx == NULL) {
192
- return;
194
+ return PBW_ERR_INVALID;
193
195
  }
194
196
  if (ctx->arena.no_grow) {
195
197
  /* Caller memory can never grow: pooling it would hand out a context
196
198
  * that fails where a pooled one succeeds, so it stays with its
197
199
  * caller. */
198
- return;
200
+ return PBW_ERR_INVALID;
199
201
  }
200
202
  /* A released context is always clean: the next acquire can use it
201
203
  * without resetting. The retain cap bounds what the reset keeps, so
@@ -203,7 +205,7 @@ pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx)
203
205
  pbw_ctx_reset(ctx);
204
206
  if (pool->count >= pool->cap) {
205
207
  pbw_ctx_free(ctx);
206
- return;
208
+ return PBW_OK;
207
209
  }
208
210
  /* Grow the slot array by doubling: one realloc per doubling instead of
209
211
  * one per release, which matters for a pool that fills up. */
@@ -212,7 +214,7 @@ pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx)
212
214
 
213
215
  if (pool->idle_cap > SIZE_MAX / 2 / sizeof(*slot)) {
214
216
  pbw_ctx_free(ctx);
215
- return;
217
+ return PBW_ERR_NOMEM;
216
218
  }
217
219
  want = pool->idle_cap != 0 ? pool->idle_cap * 2 : 8;
218
220
  if (want > pool->cap) {
@@ -220,17 +222,18 @@ pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx)
220
222
  }
221
223
  if (want <= pool->idle_cap || want > SIZE_MAX / sizeof(*slot)) {
222
224
  pbw_ctx_free(ctx);
223
- return;
225
+ return PBW_ERR_NOMEM;
224
226
  }
225
227
  slot = (pbw_ctx **)realloc(pool->idle, want * sizeof(*slot));
226
228
  if (slot == NULL) {
227
229
  pbw_ctx_free(ctx);
228
- return;
230
+ return PBW_ERR_NOMEM;
229
231
  }
230
232
  pool->idle = slot;
231
233
  pool->idle_cap = want;
232
234
  }
233
235
  pool->idle[pool->count++] = ctx;
236
+ return PBW_OK;
234
237
  }
235
238
 
236
239
  size_t
@@ -295,6 +298,11 @@ pbw_encode_ctx(pbw_ctx *ctx, const pbw_message *msg, const uint8_t **out,
295
298
  }
296
299
  *out = NULL;
297
300
  *len = 0;
301
+ if (ctx->out.fixed) {
302
+ /* A caller-memory context cannot grow an encode buffer: encoding
303
+ * from it is pbw_encode_to() into caller memory, not this call. */
304
+ return PBW_ERR_INVALID;
305
+ }
298
306
  status = pbw_encode_buf(&ctx->out, msg);
299
307
  if (status != PBW_OK) {
300
308
  *out = NULL;
@@ -802,6 +802,10 @@ read_message_field(reader *r, const pbw_field *field, pbw_message *msg,
802
802
  pbw_value *item = pbw_value_new_message_a(msg->arena, nested);
803
803
 
804
804
  if (item == NULL) {
805
+ /* decode_message marks nested results stored; the item that
806
+ * would own it never existed, so drop the mark and release
807
+ * it instead of leaking the temp. */
808
+ nested->stored = 0;
805
809
  pbw_message_free(nested);
806
810
  return PBW_ERR_NOMEM;
807
811
  }
@@ -937,10 +941,14 @@ messageset_merge(pbw_message *dst, pbw_message *src)
937
941
  messageset_merge(dst->values[i].as.msg, schild);
938
942
 
939
943
  pbw_msg_set_present(src, i, 0);
944
+ /* The sub-merge detached every slot it moved, so the child is
945
+ * garbage either way: drop the stored mark and release it
946
+ * instead of leaking it, then report the merge status. */
947
+ schild->stored = 0;
948
+ pbw_message_free(schild);
940
949
  if (status != PBW_OK) {
941
950
  return status;
942
951
  }
943
- pbw_message_free(schild);
944
952
  continue;
945
953
  }
946
954
  pbw_message_put(dst, i, sv);
@@ -1062,8 +1070,12 @@ read_messageset_item(reader *r, pbw_message *msg, uint32_t depth,
1062
1070
  status = decode_message(msg->arena, ext->sub, payload, payload_len,
1063
1071
  &nested, depth + 1, dlim);
1064
1072
  if (status != PBW_OK) {
1065
- /* Undecodable payload: keep today's verdict (accept as unknown). */
1066
1073
  pbw_message_free(nested);
1074
+ if (status != PBW_ERR_INVALID) {
1075
+ /* Only a malformed payload falls back to unknown preservation;
1076
+ * the caller's depth cap or an allocation failure must surface. */
1077
+ return status;
1078
+ }
1067
1079
  return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1068
1080
  start);
1069
1081
  }
@@ -1071,8 +1083,12 @@ read_messageset_item(reader *r, pbw_message *msg, uint32_t depth,
1071
1083
  msg->values[index].kind == PBW_VALUE_MESSAGE &&
1072
1084
  msg->values[index].as.msg != NULL &&
1073
1085
  msg->values[index].as.msg->desc == ext->sub) {
1074
- /* Singular messages merge, MessageSet items included. */
1086
+ /* Singular messages merge, MessageSet items included. The merge
1087
+ * detaches the slots it moves, so the temp is garbage either way:
1088
+ * drop the stored mark decode_message set and release it before
1089
+ * checking the status. */
1075
1090
  status = messageset_merge(msg->values[index].as.msg, nested);
1091
+ nested->stored = 0;
1076
1092
  pbw_message_free(nested);
1077
1093
  if (status != PBW_OK) {
1078
1094
  return status;
@@ -1401,6 +1417,10 @@ decode_fields(pbw_message *msg, const uint8_t *buf, size_t len, uint32_t depth,
1401
1417
  }
1402
1418
  item = pbw_value_new_message_a(msg->arena, nested);
1403
1419
  if (item == NULL) {
1420
+ /* decode_message marks nested results stored; the item
1421
+ * that would own it never existed, so drop the mark
1422
+ * and release it instead of leaking the temp. */
1423
+ nested->stored = 0;
1404
1424
  pbw_message_free(nested);
1405
1425
  return PBW_ERR_NOMEM;
1406
1426
  }
@@ -1533,7 +1553,10 @@ pbw_decode_limits(const pbw_descriptor *desc, const void *buf, size_t len,
1533
1553
  }
1534
1554
  {
1535
1555
  /* First-chunk hint from the wire length. Saturate instead of wrapping
1536
- * for a length so large that the size arithmetic would overflow. */
1556
+ * for a length so large that the size arithmetic would overflow, and
1557
+ * cap the first chunk at 64 KiB: an uncapped hint turns one huge
1558
+ * input (NULL limits) into one huge malloc up front, while the
1559
+ * chunk growth behind it already converges geometrically. */
1537
1560
  size_t hint = len;
1538
1561
 
1539
1562
  if (len <= 4096) {
@@ -1541,6 +1564,9 @@ pbw_decode_limits(const pbw_descriptor *desc, const void *buf, size_t len,
1541
1564
  } else if (len <= SIZE_MAX - len / 2 - 512) {
1542
1565
  hint = len + len / 2 + 512;
1543
1566
  }
1567
+ if (hint > ((size_t)64 << 10)) {
1568
+ hint = (size_t)64 << 10;
1569
+ }
1544
1570
  arena = pbw_arena_new_packed(hint);
1545
1571
  }
1546
1572
  if (arena == NULL) {
@@ -18,8 +18,13 @@
18
18
  #include <stdarg.h>
19
19
  #include <stdio.h>
20
20
 
21
- /* buffer size for a fully qualified name; longer names are rejected */
22
- #define DS_MAX_NAME 512
21
+ /* Fully qualified names (package plus nesting, e.g. "a.b.Outer.Inner") can
22
+ * legitimately exceed 512 bytes, so allow 4096. Every holder is either a
23
+ * short-lived stack slot (safe: recursion is capped at PBW_MAX_DEPTH and the
24
+ * ~16 KiB ds_field_info already lives on the heap) or heap-allocated
25
+ * (ds_field_info, ds_pending_ext); longer names are rejected with an
26
+ * explicit "name too long" error. */
27
+ #define DS_MAX_NAME 4096
23
28
  /* proto2 defaults are short literals; longer ones are rejected */
24
29
  #define DS_MAX_DEFAULT 4096
25
30
 
@@ -38,6 +43,10 @@ typedef struct {
38
43
  int repack;
39
44
  int msg_enc;
40
45
  int utf8;
46
+ /* Dialect of the file that declared the field, for the default behind
47
+ * those five: an extension may be declared in a proto3 file while its
48
+ * extendee is a proto2 message. */
49
+ int proto3;
41
50
  } ds_features;
42
51
 
43
52
  /* One parsed FieldDescriptorProto: regular fields and extensions share the
@@ -87,10 +96,73 @@ static void
87
96
  ds_error(pbw_schema *schema, const char *fmt, ...)
88
97
  {
89
98
  va_list ap;
99
+ /* New message, formatted into a side buffer first so a truncation or a
100
+ * later error never tears the committed first error. */
101
+ char msg[sizeof(schema->error)];
102
+ int n;
90
103
 
104
+ if (schema == NULL || fmt == NULL) {
105
+ return;
106
+ }
91
107
  va_start(ap, fmt);
92
- vsnprintf(schema->error, sizeof(schema->error), fmt, ap);
108
+ n = vsnprintf(msg, sizeof(msg), fmt, ap);
93
109
  va_end(ap);
110
+ if (n < 0) {
111
+ return;
112
+ }
113
+ if (schema->error[0] == '\0') {
114
+ /* First error wins verbatim. A single over-long message is already
115
+ * truncated by the vsnprintf above; mark the cut so it is not
116
+ * mistaken for the whole text. */
117
+ memcpy(schema->error, msg, sizeof(schema->error));
118
+ if (n >= (int)sizeof(schema->error) && sizeof(schema->error) > 4) {
119
+ memcpy(schema->error + sizeof(schema->error) - 4, "...", 4);
120
+ }
121
+ return;
122
+ }
123
+ /* Later errors are suppressed: keep the first message's prefix and only
124
+ * bump the omission count, so the buffer always shows the first failure
125
+ * plus how many followed. The count lives in the tail text itself, so no
126
+ * struct growth (and no ABI change) is needed. */
127
+ {
128
+ unsigned suppressed = 0;
129
+ size_t base_len;
130
+ char suffix[64];
131
+ int slen;
132
+ char *tail = strstr(schema->error, " ... (+");
133
+
134
+ if (tail != NULL) {
135
+ size_t tail_off = (size_t)(tail - schema->error);
136
+ size_t tail_len = strlen(tail);
137
+ /* Suffix is always ASCII at the very end: " ... (+N more)". */
138
+ if (tail_len > 9 &&
139
+ strcmp(tail + tail_len - 6, " more)") == 0 &&
140
+ sscanf(tail, " ... (+%u more)", &suppressed) == 1) {
141
+ base_len = tail_off;
142
+ } else {
143
+ base_len = strlen(schema->error);
144
+ suppressed = 0;
145
+ }
146
+ } else {
147
+ base_len = strlen(schema->error);
148
+ suppressed = 0;
149
+ }
150
+ if (suppressed >= (unsigned)~0u - 1u) {
151
+ return; /* counter saturated: keep the committed text as is */
152
+ }
153
+ slen = snprintf(suffix, sizeof(suffix), " ... (+%u more)",
154
+ suppressed + 1);
155
+ if (slen < 0) {
156
+ return;
157
+ }
158
+ if ((size_t)slen >= sizeof(suffix)) {
159
+ return; /* cannot happen: bounded %u text */
160
+ }
161
+ if (base_len > sizeof(schema->error) - 1 - (size_t)slen) {
162
+ base_len = sizeof(schema->error) - 1 - (size_t)slen;
163
+ }
164
+ memcpy(schema->error + base_len, suffix, (size_t)slen + 1);
165
+ }
94
166
  }
95
167
 
96
168
  /* Reads the next field. 1 = field, 0 = end, -1 = malformed. Legacy groups never
@@ -507,7 +579,9 @@ ds_add_field(pbw_schema *schema, pbw_descriptor *msg,
507
579
  field->presence = 1;
508
580
  }
509
581
  field->enum_open =
510
- (uint8_t)info->feat.enum_open; /* closed-ness lives on the enum type */
582
+ (uint8_t)(info->type == PBW_TYPE_ENUM
583
+ ? info->feat.enum_open /* closed-ness lives on the enum type */
584
+ : 0);
511
585
  field->msg_enc =
512
586
  (uint8_t)(info->feat.msg_enc ? info->feat.msg_enc : file->msg_enc);
513
587
  /* utf8_validation, the reference's rule: field FeatureSet, then the file's,
@@ -516,13 +590,15 @@ ds_add_field(pbw_schema *schema, pbw_descriptor *msg,
516
590
  {
517
591
  int utf8 = info->feat.utf8 ? info->feat.utf8
518
592
  : (file->utf8 ? file->utf8
519
- : (msg->proto3 ? 2 : 3));
593
+ : (file->proto3 ? 2 : 3));
520
594
 
521
595
  field->utf8_check =
522
596
  (uint8_t)(utf8 == 2 && info->type == PBW_TYPE_STRING);
523
597
  }
524
598
  if (info->has_oneof) {
525
- pbw_field_set_oneof(field, info->oneof);
599
+ if ((status = pbw_field_set_oneof(field, info->oneof)) != PBW_OK) {
600
+ return status;
601
+ }
526
602
  }
527
603
  return PBW_OK;
528
604
  }
@@ -579,8 +655,9 @@ ds_queue_extension(pbw_schema *schema, ds_pending_ext **exts, size_t *count,
579
655
  return PBW_OK;
580
656
  }
581
657
 
582
- /* Attaches every queued extension to the message it extends. The dialect
583
- * follows the extendee: an extension of a proto2 message is proto2 itself. */
658
+ /* Attaches every queued extension to the message it extends. The field's
659
+ * dialect (presence, packing, utf8_validation) follows the file that declares
660
+ * the extension, not the extendee's: the two may differ. */
584
661
  static pbw_status
585
662
  ds_attach_extensions(pbw_schema *schema, ds_pending_ext *exts, size_t count)
586
663
  {
@@ -760,6 +837,9 @@ ds_enum_proto(pbw_schema *schema, const char *parent, const uint8_t *p,
760
837
  ds_error(schema, "enum value without a name in %s", fqname);
761
838
  return PBW_ERR_INVALID;
762
839
  }
840
+ /* EnumValueDescriptorProto.number is optional (default 0), so a
841
+ * missing number is not an error; protoc never omits it, and the
842
+ * reference descriptor pool accepts the default. */
763
843
  status = pbw_enum_add_value(desc, value_name, number);
764
844
  if (status != PBW_OK) {
765
845
  ds_error(schema, "out of memory");
@@ -1153,6 +1233,12 @@ ds_service_proto(pbw_schema *schema, const char *package, const uint8_t *p,
1153
1233
  end)) != PBW_OK) {
1154
1234
  return status;
1155
1235
  }
1236
+ if (pbw_schema_service_find(schema, fqname) != NULL) {
1237
+ /* Two services under one name would make every lookup return the
1238
+ * first one and silently hide the other's methods. */
1239
+ ds_error(schema, "duplicate service %s", fqname);
1240
+ return PBW_ERR_INVALID;
1241
+ }
1156
1242
  svc = (pbw_service *)calloc(1, sizeof(*svc));
1157
1243
  if (svc == NULL) {
1158
1244
  ds_error(schema, "out of memory");
@@ -1190,7 +1276,10 @@ ds_service_proto(pbw_schema *schema, const char *package, const uint8_t *p,
1190
1276
  break;
1191
1277
  }
1192
1278
  if (info.name[0] == '\0' || info.input[0] == '\0' ||
1193
- info.output[0] == '\0') {
1279
+ info.output[0] == '\0' ||
1280
+ pbw_service_method_find(svc, info.name) != NULL) {
1281
+ /* A repeated rpc name resolves to the first method, so the
1282
+ * second one would never be reachable. */
1194
1283
  status = PBW_ERR_INVALID;
1195
1284
  break;
1196
1285
  }
@@ -1302,10 +1391,16 @@ ds_file_proto(pbw_schema *schema, const uint8_t *p, const uint8_t *end,
1302
1391
  /* File FeatureSet over the syntax defaults. FileOptions.features is
1303
1392
  * field 50; options may sit anywhere, so re-walk for them. */
1304
1393
  memset(&feat, 0, sizeof(feat));
1305
- feat.presence = has_edition ? 1 : 0; /* EXPLICIT unless overridden */
1306
- feat.enum_open = has_edition || proto3 ? 1 : 0; /* OPEN unless proto2 */
1307
- feat.repack = proto3 || has_edition; /* PACKED for proto3/editions */
1308
- feat.msg_enc = has_edition ? 1 : 0; /* LENGTH_PREFIXED for editions */
1394
+ /* Dialect defaults: the proto3 dialect (proto3 syntax, and the
1395
+ * EDITION_PROTO3 placeholder 999) has implicit presence, open enums and
1396
+ * packed repeated fields; proto2 syntax and the EDITION_PROTO2 placeholder
1397
+ * (998) have explicit presence, closed enums and expanded repeats; every
1398
+ * other edition defaults like 2023 (explicit presence, OPEN, PACKED). */
1399
+ feat.presence = (has_edition ? edition == 999 : proto3 != 0) ? 0 : 1;
1400
+ feat.enum_open = proto3 ? 1 : 0;
1401
+ feat.repack = proto3 ? 1 : 0;
1402
+ feat.msg_enc = has_edition ? 1 : 0; /* LENGTH_PREFIXED for editions */
1403
+ feat.proto3 = proto3;
1309
1404
  walk = p;
1310
1405
  for (;;) {
1311
1406
  int rc = ds_next(&walk, end, &f);
@@ -1461,7 +1556,9 @@ ds_load_impl(const void *data, size_t len, uint32_t dlim, size_t max_bytes,
1461
1556
  pbw_status
1462
1557
  pbw_descriptor_set_load(const void *data, size_t len, pbw_schema **out)
1463
1558
  {
1464
- return ds_load_impl(data, len, PBW_MAX_DEPTH, 0, out);
1559
+ /* Capped by default: an unbounded load turns one oversized read into
1560
+ * unbounded allocation. Larger trusted sets use _limits explicitly. */
1561
+ return ds_load_impl(data, len, PBW_MAX_DEPTH, PBW_DEFAULT_MAX_BYTES, out);
1465
1562
  }
1466
1563
 
1467
1564
  pbw_status
@@ -1469,7 +1566,10 @@ pbw_descriptor_set_load_limits(const void *data, size_t len,
1469
1566
  const pbw_limits *limits, pbw_schema **out)
1470
1567
  {
1471
1568
  uint32_t dlim = PBW_MAX_DEPTH;
1472
- size_t max_bytes = 0;
1569
+ /* NULL means the plain-loader cap: an explicit {0, 0} stays uncapped
1570
+ * (0 = no byte cap, per pbw_limits), but a missing struct must not
1571
+ * turn one oversized read into unbounded allocation. */
1572
+ size_t max_bytes = PBW_DEFAULT_MAX_BYTES;
1473
1573
 
1474
1574
  if (limits != NULL) {
1475
1575
  /* Above PBW_MAX_DEPTH clamps, like the wire decoder. */