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 +4 -4
- data/README.md +68 -19
- data/ext/pbwire_ext/core_ctx.c +16 -8
- data/ext/pbwire_ext/core_decode.c +30 -4
- data/ext/pbwire_ext/core_descriptor_set.c +115 -15
- data/ext/pbwire_ext/core_encode.c +122 -81
- data/ext/pbwire_ext/core_json.c +487 -123
- data/ext/pbwire_ext/core_message.c +36 -2
- data/ext/pbwire_ext/core_schema.c +69 -12
- data/ext/pbwire_ext/core_stream.c +82 -22
- data/ext/pbwire_ext/core_text.c +25 -15
- data/ext/pbwire_ext/core_wire.c +7 -3
- data/ext/pbwire_ext/core_wkt.c +3 -2
- data/ext/pbwire_ext/internal.h +32 -7
- data/ext/pbwire_ext/pbwire.h +137 -53
- data/ext/pbwire_ext/pbwire_ext.c +759 -7
- data/lib/pbwire.rb +7 -4
- metadata +16 -2
|
@@ -91,11 +91,17 @@ pbw_message_wire_size(const pbw_message *msg)
|
|
|
91
91
|
return msg == NULL ? 0 : msg->wire_len;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
/* Caller-bounded append: intentionally no max_bytes check here (decode-path
|
|
95
|
+
* unknowns are bounded by limits->max_bytes instead); repeated calls grow
|
|
96
|
+
* the store without bound, so the caller must bound total bytes/calls.
|
|
97
|
+
* shortcut: caller-bounded, upgrade to checked wrapper when gateway exposes
|
|
98
|
+
* raw append to untrusted input. */
|
|
94
99
|
int
|
|
95
100
|
pbw_message_add_unknown(pbw_message *msg, const uint8_t *data, size_t len)
|
|
96
101
|
{
|
|
97
102
|
size_t need;
|
|
98
103
|
uint8_t *grown;
|
|
104
|
+
uint8_t *stash = NULL;
|
|
99
105
|
|
|
100
106
|
if (msg == NULL) {
|
|
101
107
|
return PBW_ERR_INVALID;
|
|
@@ -123,8 +129,25 @@ pbw_message_add_unknown(pbw_message *msg, const uint8_t *data, size_t len)
|
|
|
123
129
|
}
|
|
124
130
|
cap *= 2;
|
|
125
131
|
}
|
|
132
|
+
/* The caller may append the message's own blob to itself; a heap
|
|
133
|
+
* realloc can move that block, so copy the source first. Arena
|
|
134
|
+
* memory never moves, so the copy below stays valid without one. */
|
|
135
|
+
if (msg->arena == NULL && msg->unknown != NULL && len <= (size_t)(UINTPTR_MAX - (uintptr_t)(const void *)data)) {
|
|
136
|
+
uintptr_t base = (uintptr_t)(void *)msg->unknown;
|
|
137
|
+
uintptr_t src = (uintptr_t)(const void *)data;
|
|
138
|
+
|
|
139
|
+
if (src < base + msg->unknown_len && src + len > base) {
|
|
140
|
+
stash = (uint8_t *)malloc(len);
|
|
141
|
+
if (stash == NULL) {
|
|
142
|
+
return PBW_ERR_NOMEM;
|
|
143
|
+
}
|
|
144
|
+
memcpy(stash, data, len);
|
|
145
|
+
data = stash;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
126
148
|
if (msg->arena != NULL) {
|
|
127
149
|
if (msg->unknown == NULL && msg->unknown_len > 0) {
|
|
150
|
+
free(stash);
|
|
128
151
|
return PBW_ERR_NOMEM;
|
|
129
152
|
}
|
|
130
153
|
grown = (uint8_t *)pbw_arena_alloc(msg->arena, cap);
|
|
@@ -135,13 +158,15 @@ pbw_message_add_unknown(pbw_message *msg, const uint8_t *data, size_t len)
|
|
|
135
158
|
grown = (uint8_t *)realloc(msg->unknown, cap);
|
|
136
159
|
}
|
|
137
160
|
if (grown == NULL) {
|
|
161
|
+
free(stash);
|
|
138
162
|
return PBW_ERR_NOMEM;
|
|
139
163
|
}
|
|
140
164
|
msg->unknown = grown;
|
|
141
165
|
msg->unknown_cap = cap;
|
|
142
166
|
}
|
|
143
|
-
|
|
167
|
+
memmove(msg->unknown + msg->unknown_len, data, len);
|
|
144
168
|
msg->unknown_len = need;
|
|
169
|
+
free(stash);
|
|
145
170
|
return PBW_OK;
|
|
146
171
|
}
|
|
147
172
|
|
|
@@ -612,10 +637,16 @@ pbw_value_get_kind(const pbw_value *value)
|
|
|
612
637
|
return value == NULL ? PBW_VALUE_UINT : value->kind;
|
|
613
638
|
}
|
|
614
639
|
|
|
640
|
+
/* Wrong-kind (including NULL) reads as 0 by design, indistinguishable from a
|
|
641
|
+
* genuine 0: callers must check pbw_value_get_kind() == PBW_VALUE_UINT first.
|
|
642
|
+
* No status-returning checked accessor exists; signatures stay frozen for ABI. */
|
|
615
643
|
uint64_t
|
|
616
644
|
pbw_value_get_uint(const pbw_value *value)
|
|
617
645
|
{
|
|
618
|
-
|
|
646
|
+
if (value == NULL || value->kind != PBW_VALUE_UINT) {
|
|
647
|
+
return 0;
|
|
648
|
+
}
|
|
649
|
+
return value->as.u64;
|
|
619
650
|
}
|
|
620
651
|
|
|
621
652
|
int64_t
|
|
@@ -732,6 +763,9 @@ value_matches(const pbw_field *field, const pbw_value *value)
|
|
|
732
763
|
static void
|
|
733
764
|
clear_slot(pbw_message *msg, size_t index)
|
|
734
765
|
{
|
|
766
|
+
/* Any store/clear invalidates the decode-size hint, so a shrink never
|
|
767
|
+
* over-reserves the next encode. */
|
|
768
|
+
msg->wire_len = 0;
|
|
735
769
|
if (pbw_msg_present(msg, index)) {
|
|
736
770
|
/* Absent before destroy, for the same reason the release walk does it:
|
|
737
771
|
* an arena message's struct outlives its payload. */
|
|
@@ -177,6 +177,10 @@ pbw_schema_free(pbw_schema *schema)
|
|
|
177
177
|
if (schema == NULL) {
|
|
178
178
|
return;
|
|
179
179
|
}
|
|
180
|
+
/* No live check here: schemas are shared across threads (see
|
|
181
|
+
* test_threads), so a race-free live count needs atomics portable C99
|
|
182
|
+
* does not have. The contract is documented on the declaration: the
|
|
183
|
+
* schema must outlive every message built from it. */
|
|
180
184
|
for (i = 0; i < schema->count; i++) {
|
|
181
185
|
pbw_descriptor_free(schema->descs[i]);
|
|
182
186
|
}
|
|
@@ -212,6 +216,11 @@ add_descriptor(pbw_schema *schema, const char *name,
|
|
|
212
216
|
if (schema == NULL || name == NULL || pbw_schema_find(schema, name) != NULL) {
|
|
213
217
|
return NULL;
|
|
214
218
|
}
|
|
219
|
+
if (schema->frozen) {
|
|
220
|
+
snprintf(schema->error, sizeof(schema->error),
|
|
221
|
+
"add descriptor %s before pbw_schema_link()", name);
|
|
222
|
+
return NULL;
|
|
223
|
+
}
|
|
215
224
|
if (schema->count == schema->cap) {
|
|
216
225
|
size_t cap = schema->cap ? schema->cap * 2 : 8;
|
|
217
226
|
size_t bytes;
|
|
@@ -700,16 +709,16 @@ pbw_field_set_packed(pbw_field *field, int packed)
|
|
|
700
709
|
}
|
|
701
710
|
}
|
|
702
711
|
|
|
703
|
-
|
|
712
|
+
pbw_status
|
|
704
713
|
pbw_field_set_oneof(pbw_field *field, uint32_t oneof_index)
|
|
705
714
|
{
|
|
706
715
|
if (field == NULL) {
|
|
707
|
-
return;
|
|
716
|
+
return PBW_ERR_INVALID;
|
|
708
717
|
}
|
|
709
718
|
if (field->schema != NULL && field->schema->frozen) {
|
|
710
719
|
/* Growing oneof_count after a link would leave existing messages
|
|
711
720
|
* with a oneof_case array shorter than the index they use. */
|
|
712
|
-
return;
|
|
721
|
+
return PBW_ERR_INVALID;
|
|
713
722
|
}
|
|
714
723
|
field->oneof_index = oneof_index;
|
|
715
724
|
if (field->schema != NULL) {
|
|
@@ -721,14 +730,20 @@ pbw_field_set_oneof(pbw_field *field, uint32_t oneof_index)
|
|
|
721
730
|
* retire existing messages so a later set cannot index past it. */
|
|
722
731
|
for (i = 0; i < schema->count; i++) {
|
|
723
732
|
pbw_descriptor *desc = schema->descs[i];
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
733
|
+
/* The field array of every live descriptor occupies a distinct
|
|
734
|
+
* range, so an integer range test finds the owner. Relational
|
|
735
|
+
* pointer comparison between unrelated arrays is undefined. */
|
|
736
|
+
uintptr_t lo = (uintptr_t)(void *)desc->fields;
|
|
737
|
+
uintptr_t f = (uintptr_t)(void *)field;
|
|
738
|
+
|
|
739
|
+
if (!desc->is_enum && f >= lo &&
|
|
740
|
+
f - lo < desc->field_count * sizeof(pbw_field)) {
|
|
727
741
|
desc->epoch++;
|
|
728
742
|
break;
|
|
729
743
|
}
|
|
730
744
|
}
|
|
731
745
|
}
|
|
746
|
+
return PBW_OK;
|
|
732
747
|
}
|
|
733
748
|
|
|
734
749
|
static int
|
|
@@ -1036,15 +1051,46 @@ link_message(pbw_schema *schema, pbw_descriptor *msg)
|
|
|
1036
1051
|
if (map == NULL) {
|
|
1037
1052
|
return PBW_ERR_NOMEM;
|
|
1038
1053
|
}
|
|
1054
|
+
/* Two passes: regular fields claim their name first, then extensions.
|
|
1055
|
+
* Extensions may share a name with a regular field by design, and a
|
|
1056
|
+
* plain-name lookup must find the regular one; a name only an
|
|
1057
|
+
* extension has still resolves to that extension. */
|
|
1039
1058
|
for (i = 0; i < msg->field_count; i++) {
|
|
1040
1059
|
const pbw_field *field = &msg->fields[i];
|
|
1041
|
-
size_t slot
|
|
1060
|
+
size_t slot;
|
|
1042
1061
|
|
|
1062
|
+
if (field->is_extension) {
|
|
1063
|
+
continue;
|
|
1064
|
+
}
|
|
1065
|
+
slot = (size_t)(name_hash(field->name) & (uint32_t)(size - 1));
|
|
1043
1066
|
while (map[slot] != NULL) {
|
|
1044
1067
|
slot = (slot + 1) & (size - 1);
|
|
1045
1068
|
}
|
|
1046
1069
|
map[slot] = field;
|
|
1047
1070
|
}
|
|
1071
|
+
for (i = 0; i < msg->field_count; i++) {
|
|
1072
|
+
const pbw_field *field = &msg->fields[i];
|
|
1073
|
+
size_t slot;
|
|
1074
|
+
const pbw_field *probe;
|
|
1075
|
+
|
|
1076
|
+
if (!field->is_extension) {
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
slot = (size_t)(name_hash(field->name) & (uint32_t)(size - 1));
|
|
1080
|
+
for (;;) {
|
|
1081
|
+
probe = map[slot];
|
|
1082
|
+
if (probe == NULL) {
|
|
1083
|
+
break;
|
|
1084
|
+
}
|
|
1085
|
+
if (strcmp(probe->name, field->name) == 0) {
|
|
1086
|
+
break; /* a regular field already owns the name */
|
|
1087
|
+
}
|
|
1088
|
+
slot = (slot + 1) & (size - 1);
|
|
1089
|
+
}
|
|
1090
|
+
if (probe == NULL) {
|
|
1091
|
+
map[slot] = field;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1048
1094
|
msg->name_map = map;
|
|
1049
1095
|
msg->name_map_size = size;
|
|
1050
1096
|
}
|
|
@@ -1196,14 +1242,23 @@ pbw_descriptor_find_field_name(const pbw_descriptor *desc,
|
|
|
1196
1242
|
}
|
|
1197
1243
|
{
|
|
1198
1244
|
size_t i;
|
|
1245
|
+
const pbw_field *match = NULL;
|
|
1199
1246
|
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1247
|
+
/* Regular fields win a shared name; extensions fill the rest. */
|
|
1248
|
+
for (i = 0; i < desc->field_count && match == NULL; i++) {
|
|
1249
|
+
if (!desc->fields[i].is_extension &&
|
|
1250
|
+
strcmp(desc->fields[i].name, name) == 0) {
|
|
1251
|
+
match = &desc->fields[i];
|
|
1203
1252
|
}
|
|
1204
1253
|
}
|
|
1254
|
+
for (i = 0; i < desc->field_count && match == NULL; i++) {
|
|
1255
|
+
if (desc->fields[i].is_extension &&
|
|
1256
|
+
strcmp(desc->fields[i].name, name) == 0) {
|
|
1257
|
+
match = &desc->fields[i];
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return match;
|
|
1205
1261
|
}
|
|
1206
|
-
return NULL;
|
|
1207
1262
|
}
|
|
1208
1263
|
|
|
1209
1264
|
const char *
|
|
@@ -1446,7 +1501,9 @@ pbw_service_resolve(const pbw_schema *schema, const char *service,
|
|
|
1446
1501
|
}
|
|
1447
1502
|
in = pbw_schema_find(schema, pbw_method_input_type(m));
|
|
1448
1503
|
out = pbw_schema_find(schema, pbw_method_output_type(m));
|
|
1449
|
-
if (in == NULL || out == NULL) {
|
|
1504
|
+
if (in == NULL || out == NULL || in->is_enum || out->is_enum) {
|
|
1505
|
+
/* RPC types are messages; an enum descriptor has no fields to drive
|
|
1506
|
+
* a request or a reply. */
|
|
1450
1507
|
return PBW_ERR_INVALID;
|
|
1451
1508
|
}
|
|
1452
1509
|
if (in_desc != NULL) {
|
|
@@ -27,6 +27,12 @@ pbw_istream_new(const pbw_limits *limits)
|
|
|
27
27
|
}
|
|
28
28
|
if (limits != NULL) {
|
|
29
29
|
s->limits = *limits;
|
|
30
|
+
} else {
|
|
31
|
+
/* Capped by default: an unbounded stream buffer turns a sender
|
|
32
|
+
* that never completes a frame into unbounded growth. Trusted
|
|
33
|
+
* input that must stream uncapped passes explicit limits with
|
|
34
|
+
* max_bytes = 0. */
|
|
35
|
+
s->limits.max_bytes = PBW_DEFAULT_MAX_BYTES;
|
|
30
36
|
}
|
|
31
37
|
return s;
|
|
32
38
|
}
|
|
@@ -51,12 +57,12 @@ pbw_istream_reset(pbw_istream *s)
|
|
|
51
57
|
s->off = 0;
|
|
52
58
|
}
|
|
53
59
|
|
|
54
|
-
/* True when the bytes hold at least one complete frame (length
|
|
55
|
-
* body), without decoding anything. Lets feed() tell pipelined
|
|
56
|
-
* (which next() will drain and police) from never-completing bytes
|
|
57
|
-
* would otherwise grow the buffer forever). */
|
|
60
|
+
/* True when the bytes hold at least one complete in-cap frame (length
|
|
61
|
+
* prefix plus body), without decoding anything. Lets feed() tell pipelined
|
|
62
|
+
* frames (which next() will drain and police) from never-completing bytes
|
|
63
|
+
* (which would otherwise grow the buffer forever). */
|
|
58
64
|
static int
|
|
59
|
-
has_complete_frame(const uint8_t *base, size_t avail)
|
|
65
|
+
has_complete_frame(const uint8_t *base, size_t avail, size_t max)
|
|
60
66
|
{
|
|
61
67
|
size_t used = 0;
|
|
62
68
|
size_t frame;
|
|
@@ -67,6 +73,9 @@ has_complete_frame(const uint8_t *base, size_t avail)
|
|
|
67
73
|
if (!pbw_len_read(base, base + avail, &frame, &used)) {
|
|
68
74
|
return 0;
|
|
69
75
|
}
|
|
76
|
+
if (frame > max) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
70
79
|
return used <= avail && frame <= avail - used;
|
|
71
80
|
}
|
|
72
81
|
|
|
@@ -81,9 +90,10 @@ pbw_istream_feed(pbw_istream *s, const void *data, size_t len)
|
|
|
81
90
|
}
|
|
82
91
|
/* Bound never-completing bytes: without a cap a sender that never forms
|
|
83
92
|
* a frame grows the buffer forever. Pipelined complete frames stay
|
|
84
|
-
* allowed past the cap (next() polices each frame); only a
|
|
85
|
-
* no complete frame in it is refused. The +5 is the
|
|
86
|
-
* a max-sized frame needs at most max_bytes + 5
|
|
93
|
+
* allowed past the cap (next() drains and polices each frame); only a
|
|
94
|
+
* buffer with no complete in-cap frame in it is refused. The +5 is the
|
|
95
|
+
* length-prefix slack: a max-sized frame needs at most max_bytes + 5
|
|
96
|
+
* buffered bytes. */
|
|
87
97
|
if (s->limits.max_bytes > 0) {
|
|
88
98
|
size_t max = s->limits.max_bytes;
|
|
89
99
|
size_t buffered = s->len - s->off;
|
|
@@ -102,25 +112,73 @@ pbw_istream_feed(pbw_istream *s, const void *data, size_t len)
|
|
|
102
112
|
const uint8_t *base =
|
|
103
113
|
(buffered > 0 && s->data != NULL) ? s->data + s->off : NULL;
|
|
104
114
|
|
|
105
|
-
if (base != NULL && has_complete_frame(base, buffered)) {
|
|
115
|
+
if (base != NULL && has_complete_frame(base, buffered, max)) {
|
|
106
116
|
/* Pipelined: next() drains each frame and polices it. */
|
|
107
117
|
} else if (buffered == 0) {
|
|
108
|
-
|
|
118
|
+
/*
|
|
119
|
+
* Empty buffer: accept one bounded pipelined batch so a feed
|
|
120
|
+
* holding several complete frames makes progress instead of
|
|
121
|
+
* livelocking (feed LIMIT, next NULL, re-feed LIMIT); next()
|
|
122
|
+
* drains it one frame at a time. The bound keeps the worst
|
|
123
|
+
* case near twice the cap, and the batch must open with an
|
|
124
|
+
* in-cap frame, so a single over-cap frame (or a corrupt
|
|
125
|
+
* prefix) is still refused here. Anything larger arrives in
|
|
126
|
+
* slices of max_bytes + 5, which always fit an empty buffer.
|
|
127
|
+
*/
|
|
128
|
+
const uint8_t *chunk = (const uint8_t *)data;
|
|
129
|
+
size_t bound =
|
|
130
|
+
max > (SIZE_MAX - 5) / 2 ? SIZE_MAX : 2 * max + 5;
|
|
131
|
+
size_t first = 0;
|
|
132
|
+
size_t head = 0;
|
|
133
|
+
|
|
134
|
+
if (len > bound ||
|
|
135
|
+
!pbw_len_read(chunk, chunk + len, &first, &head) ||
|
|
136
|
+
first > max) {
|
|
109
137
|
return PBW_ERR_LIMIT;
|
|
110
138
|
}
|
|
111
139
|
} else {
|
|
112
|
-
/* A split frame is only let through when this chunk
|
|
113
|
-
* completes a within-cap frame; anything else (a partial
|
|
114
|
-
* prefix, an oversized frame, trailing garbage) is refused
|
|
115
|
-
* before it grows the buffer. */
|
|
116
140
|
size_t used = 0;
|
|
117
141
|
size_t frame = 0;
|
|
142
|
+
int have = 0;
|
|
143
|
+
int need_more = 0;
|
|
118
144
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
145
|
+
/* Past max_bytes + 5 only the chunk completing a split frame
|
|
146
|
+
* (plus one cap window of trailing) grows the buffer. */
|
|
147
|
+
if (base != NULL) {
|
|
148
|
+
have =
|
|
149
|
+
pbw_len_read(base, base + buffered, &frame, &used);
|
|
150
|
+
if (!have && buffered < 5) {
|
|
151
|
+
/* Split prefix: buffered ends mid-varint, so a read of
|
|
152
|
+
* buffered alone always fails even for an in-cap
|
|
153
|
+
* frame. Complete it with prefix bytes from the chunk
|
|
154
|
+
* (at most the 5-byte varint max) and judge the merged
|
|
155
|
+
* prefix instead. */
|
|
156
|
+
uint8_t window[5];
|
|
157
|
+
size_t need = 5 - buffered;
|
|
158
|
+
size_t take = len < need ? len : need;
|
|
159
|
+
size_t avail = buffered + take;
|
|
160
|
+
const uint8_t *chunk = (const uint8_t *)data;
|
|
161
|
+
|
|
162
|
+
memcpy(window, base, buffered);
|
|
163
|
+
memcpy(window + buffered, chunk, take);
|
|
164
|
+
if (pbw_len_read(window, window + avail, &frame,
|
|
165
|
+
&used)) {
|
|
166
|
+
have = 1;
|
|
167
|
+
} else if (avail < 5) {
|
|
168
|
+
/* Fewer than 5 bytes total: still truncated, need
|
|
169
|
+
* more to decide; let it buffer. Unreachable past
|
|
170
|
+
* the cap (over implies len fills to 5), but keeps
|
|
171
|
+
* the prefix-completion semantics. */
|
|
172
|
+
need_more = 1;
|
|
173
|
+
have = 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!have || (!need_more &&
|
|
178
|
+
(frame > max || used > max + 5 - frame ||
|
|
179
|
+
used + frame <= buffered ||
|
|
180
|
+
used + frame > buffered + len ||
|
|
181
|
+
buffered + len - used - frame > max + 5))) {
|
|
124
182
|
return PBW_ERR_LIMIT;
|
|
125
183
|
}
|
|
126
184
|
}
|
|
@@ -352,12 +410,14 @@ frame_split(const void *buf, size_t len, const pbw_limits *limits,
|
|
|
352
410
|
if (!pbw_len_read(base, base + len, frame, used)) {
|
|
353
411
|
return PBW_ERR_INVALID;
|
|
354
412
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
}
|
|
413
|
+
/* The cap is checked before the body, so an oversized frame is LIMIT
|
|
414
|
+
* whether or not the bytes arrived -- same order as pbw_istream_next(). */
|
|
358
415
|
if (limits != NULL && limits->max_bytes > 0 && *frame > limits->max_bytes) {
|
|
359
416
|
return PBW_ERR_LIMIT;
|
|
360
417
|
}
|
|
418
|
+
if (*used > len || *frame > len - *used) {
|
|
419
|
+
return PBW_ERR_INVALID;
|
|
420
|
+
}
|
|
361
421
|
*body = base + *used;
|
|
362
422
|
return PBW_OK;
|
|
363
423
|
}
|
data/ext/pbwire_ext/core_text.c
CHANGED
|
@@ -1009,13 +1009,10 @@ t_parse_number(text_cur *c, t_number *out)
|
|
|
1009
1009
|
r++;
|
|
1010
1010
|
}
|
|
1011
1011
|
if (r >= c->end || !t_is_digit(*r)) {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
out->len = (size_t)(q - c->p);
|
|
1017
|
-
c->p = q;
|
|
1018
|
-
return 1;
|
|
1012
|
+
/* A dangling exponent is a malformed number, never a
|
|
1013
|
+
* mantissa followed by a new token: the reference lexer
|
|
1014
|
+
* rejects `.5e` as one bad float. */
|
|
1015
|
+
return 0;
|
|
1019
1016
|
}
|
|
1020
1017
|
while (r < c->end && t_is_digit(*r)) {
|
|
1021
1018
|
r++;
|
|
@@ -1122,7 +1119,9 @@ t_unescape(pbw_buf *out, const char *start, size_t len)
|
|
|
1122
1119
|
for (i = 0; i < 2 && p < end && *p >= '0' && *p <= '7'; i++) {
|
|
1123
1120
|
v = v * 8 + (unsigned)(*p++ - '0');
|
|
1124
1121
|
}
|
|
1125
|
-
/*
|
|
1122
|
+
/* The reference text tokenizer truncates an octal escape above
|
|
1123
|
+
* \\377 into a char (tokenizer.cc: static_cast<char>(code)),
|
|
1124
|
+
* unlike its CUnescape helper; match the tokenizer. */
|
|
1126
1125
|
if (!pbw_buf_reserve(out, 1)) {
|
|
1127
1126
|
return PBW_ERR_NOMEM;
|
|
1128
1127
|
}
|
|
@@ -1661,7 +1660,7 @@ t_parse_enum_value(text_in *t, pbw_message *msg, const pbw_field *field)
|
|
|
1661
1660
|
} else {
|
|
1662
1661
|
const char *start;
|
|
1663
1662
|
size_t len;
|
|
1664
|
-
char name[
|
|
1663
|
+
char name[PBW_NAME_BUF];
|
|
1665
1664
|
|
|
1666
1665
|
if (!t_ident(&t->cur, &start, &len)) {
|
|
1667
1666
|
return PBW_ERR_INVALID;
|
|
@@ -1720,7 +1719,10 @@ t_parse_string_value(text_in *t, pbw_message *msg, const pbw_field *field)
|
|
|
1720
1719
|
}
|
|
1721
1720
|
}
|
|
1722
1721
|
}
|
|
1723
|
-
|
|
1722
|
+
/* Same rule as the binary decoder: only a field whose utf8_validation
|
|
1723
|
+
* resolves to VERIFY rejects bytes, so a proto2 string the decoder
|
|
1724
|
+
* accepted prints and parses back. */
|
|
1725
|
+
if (pbw_field_requires_utf8_validation(field) &&
|
|
1724
1726
|
!pbw_utf8_valid(out.data == NULL ? (const uint8_t *)"" : out.data,
|
|
1725
1727
|
out.len)) {
|
|
1726
1728
|
pbw_buf_free(&out);
|
|
@@ -1909,8 +1911,16 @@ t_skip_value(text_in *t, uint32_t depth)
|
|
|
1909
1911
|
t->cur.p++;
|
|
1910
1912
|
t_skip(&t->cur);
|
|
1911
1913
|
}
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
+
{
|
|
1915
|
+
/* A malformed number ("1z") advances the cursor before failing,
|
|
1916
|
+
* so retry the identifier from the saved position instead of
|
|
1917
|
+
* accepting the glued tail as a separate token. */
|
|
1918
|
+
text_cur save = t->cur;
|
|
1919
|
+
|
|
1920
|
+
if (t_parse_number(&t->cur, &num)) {
|
|
1921
|
+
return PBW_OK;
|
|
1922
|
+
}
|
|
1923
|
+
t->cur = save;
|
|
1914
1924
|
}
|
|
1915
1925
|
if (t_ident(&t->cur, &start, &len)) {
|
|
1916
1926
|
return PBW_OK;
|
|
@@ -2002,13 +2012,13 @@ static pbw_status t_parse_message_body(text_in *t, pbw_message *msg,
|
|
|
2002
2012
|
* it). `reserved` reports a name the descriptor declared reserved, which the
|
|
2003
2013
|
* parser skips. The name buffers live in this non-recursive frame: the
|
|
2004
2014
|
* recursive body does not pay for them, and they are sized for the longest
|
|
2005
|
-
* identifier the descriptor loader accepts (
|
|
2015
|
+
* identifier the descriptor loader accepts (PBW_NAME_BUF). */
|
|
2006
2016
|
static pbw_status
|
|
2007
2017
|
t_lookup_field(const pbw_message *msg, const char *name, size_t name_len,
|
|
2008
2018
|
const pbw_field **out, int *reserved)
|
|
2009
2019
|
{
|
|
2010
|
-
char tmp[
|
|
2011
|
-
char lower[
|
|
2020
|
+
char tmp[PBW_NAME_BUF];
|
|
2021
|
+
char lower[PBW_NAME_BUF];
|
|
2012
2022
|
size_t i;
|
|
2013
2023
|
|
|
2014
2024
|
*out = NULL;
|
data/ext/pbwire_ext/core_wire.c
CHANGED
|
@@ -696,9 +696,13 @@ pbw_format_g_c(char *buf, size_t cap, int precision, double value)
|
|
|
696
696
|
seplen++;
|
|
697
697
|
}
|
|
698
698
|
}
|
|
699
|
-
/*
|
|
700
|
-
*
|
|
701
|
-
* length
|
|
699
|
+
/* A locale whose decimal_point is a truncated UTF-8 lead byte makes the
|
|
700
|
+
* inferred width longer than the bytes snprintf wrote; treating it as one
|
|
701
|
+
* byte keeps the memmove length from wrapping. */
|
|
702
|
+
if ((size_t)(p - buf) >= (size_t)n ||
|
|
703
|
+
seplen > (size_t)n - (size_t)(p - buf)) {
|
|
704
|
+
seplen = 1;
|
|
705
|
+
}
|
|
702
706
|
*p = '.';
|
|
703
707
|
memmove(p + 1, p + seplen,
|
|
704
708
|
(size_t)n - (size_t)(p - buf) - seplen + 1);
|
data/ext/pbwire_ext/core_wkt.c
CHANGED
|
@@ -20,7 +20,8 @@ pbw_any_pack(const pbw_descriptor *any_desc, const char *type_url,
|
|
|
20
20
|
return PBW_ERR_INVALID;
|
|
21
21
|
}
|
|
22
22
|
*out = NULL;
|
|
23
|
-
if (any_desc == NULL ||
|
|
23
|
+
if (any_desc == NULL || pbw_descriptor_is_enum(any_desc) ||
|
|
24
|
+
type_url == NULL || (payload == NULL && len > 0)) {
|
|
24
25
|
return PBW_ERR_INVALID;
|
|
25
26
|
}
|
|
26
27
|
msg = pbw_message_new(any_desc);
|
|
@@ -175,7 +176,7 @@ wkt_from_millis(const pbw_descriptor *desc, int64_t ms, int floor_nanos,
|
|
|
175
176
|
return PBW_ERR_INVALID;
|
|
176
177
|
}
|
|
177
178
|
*out = NULL;
|
|
178
|
-
if (desc == NULL) {
|
|
179
|
+
if (desc == NULL || pbw_descriptor_is_enum(desc)) {
|
|
179
180
|
return PBW_ERR_INVALID;
|
|
180
181
|
}
|
|
181
182
|
/* The pair must convert back: secs * 1000 has to stay inside int64, and
|
data/ext/pbwire_ext/internal.h
CHANGED
|
@@ -23,6 +23,18 @@
|
|
|
23
23
|
#define PBW_MAX_FIELD_NUMBER 536870911u /* 2^29 - 1 */
|
|
24
24
|
#define PBW_FNUM_MAP_LIMIT 512
|
|
25
25
|
|
|
26
|
+
/* Longest single name component the descriptor loader accepts (mirrors
|
|
27
|
+
* DS_MAX_NAME in descriptor_set.c). JSON/text name buffers derive from
|
|
28
|
+
* this so a legal 600-byte (or up to ~4 KiB) field/enum/message name
|
|
29
|
+
* round-trips instead of hitting INVALID at the old 512/520 caps. */
|
|
30
|
+
#define PBW_MAX_NAME 4096
|
|
31
|
+
/* NUL-terminated single-name buffer (field, enum value, Any type name). */
|
|
32
|
+
#define PBW_NAME_BUF PBW_MAX_NAME
|
|
33
|
+
/* NUL-terminated "[full.name]" buffer: a full extension name composes
|
|
34
|
+
* scope + '.' + name (see ds_attach_extensions full[DS_MAX_NAME * 2]),
|
|
35
|
+
* plus the two brackets. */
|
|
36
|
+
#define PBW_EXT_KEY_BUF (2 * PBW_MAX_NAME + 4)
|
|
37
|
+
|
|
26
38
|
/* Wire types. */
|
|
27
39
|
#define PBW_WIRE_VARINT 0
|
|
28
40
|
#define PBW_WIRE_64BIT 1
|
|
@@ -281,7 +293,17 @@ struct pbw_schema {
|
|
|
281
293
|
* message's value slots always match its descriptor. */
|
|
282
294
|
int frozen;
|
|
283
295
|
int proto3; /* dialect handed to descriptors created from now on */
|
|
284
|
-
|
|
296
|
+
/* Freeing the schema while messages built from it are still alive is
|
|
297
|
+
* use-after-free (see pbw_schema_free): messages keep raw descriptor
|
|
298
|
+
* pointers, so the schema must outlive them. A per-schema live count
|
|
299
|
+
* cannot enforce this: schemas are shared across threads (see
|
|
300
|
+
* test_threads), and portable C99 has no atomics for a race-free
|
|
301
|
+
* counter, so the contract is documented, not counted. */
|
|
302
|
+
/* Last link failure reason (first error wins, later ones only bump the
|
|
303
|
+
* "... (+N more)" omission count appended by ds_error, so the 512-byte
|
|
304
|
+
* buffer always shows the first failure plus how many were suppressed).
|
|
305
|
+
* Empty when the last link succeeded. */
|
|
306
|
+
char error[512];
|
|
285
307
|
};
|
|
286
308
|
|
|
287
309
|
/* pbw_value.flags: the struct or its payload was allocated from an arena or
|
|
@@ -322,8 +344,9 @@ struct pbw_message {
|
|
|
322
344
|
size_t unknown_cap;
|
|
323
345
|
/* Wire bytes this tree was loaded from (root only, 0 = unknown).
|
|
324
346
|
* encode_alloc() uses it as the initial buffer guess: exact for an
|
|
325
|
-
* untouched tree
|
|
326
|
-
*
|
|
347
|
+
* untouched tree. Any mutation via clear_slot() zeroes it, so a
|
|
348
|
+
* shrink never over-reserves the next encode (the writer still grows
|
|
349
|
+
* on demand when the hint underestimates). Set at decode. */
|
|
327
350
|
size_t wire_len;
|
|
328
351
|
};
|
|
329
352
|
|
|
@@ -409,10 +432,12 @@ pbw_varint_size(uint64_t value)
|
|
|
409
432
|
}
|
|
410
433
|
size_t pbw_varint_write(uint8_t *out, uint64_t value);
|
|
411
434
|
/* Decimal conversions for the JSON and text formats. The wire format defines
|
|
412
|
-
* '.' as the decimal separator, full stop:
|
|
413
|
-
* locale (
|
|
414
|
-
*
|
|
415
|
-
*
|
|
435
|
+
* '.' as the decimal separator, full stop: parsing never consults the host
|
|
436
|
+
* locale (custom strtod, '.' only, no localeconv), so no concurrent setlocale
|
|
437
|
+
* can skew a parse; printing formats with snprintf("%g") and replaces a
|
|
438
|
+
* foreign decimal separator with '.', so output stays byte-identical under
|
|
439
|
+
* any LC_NUMERIC without querying locale state. The first 25 significant
|
|
440
|
+
* digits accumulate into a (hi, lo)
|
|
416
441
|
* pair (error ~2^-100 relative) scaled by exact x10 steps, so ordinary
|
|
417
442
|
* inputs round exactly once, like strtod; saturation (inf/zero) is decided
|
|
418
443
|
* by integer decimal-exponent math, so DBL_MAX/DBL_MIN parse exactly.
|