include/punk_observe/po_pb_read.h

filter: Statementstmt 90.2% 93 / 103 Branchbran 67.1% 43 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.4% 136 / 167 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_expr.h ↑ Summary include/punk_observe/po_seg.h »
1 /* po_pb_read.h - a generic protobuf wire reader.
2 *
3 * The ecosystem has a writer and had no reader. otel_pb.h is 250 lines that
4 * encode correctly and have no inverse; the only decoder anywhere was 92
5 * lines of test-only Perl. Since http/protobuf is OTLP's DEFAULT transport,
6 * without this header the server cannot accept what the SDK next door sends
7 * out of the box.
8 *
9 * This mirrors otel_pb.h's shape deliberately, so the two read side by side.
10 * It knows nothing about OTLP: schema lives in po_otlp_in.h.
11 *
12 * Everything borrows. po_pbr_bytes hands back a pointer INTO the caller's
13 * buffer and copies nothing, so decoding a 4MB batch costs about one pass
14 * over 4MB and the whole ingest path holds one allocation.
15 */
16 #ifndef PO_PB_READ_H
17 #define PO_PB_READ_H
18  
19 #include "punk_observe/po_compat.h"
20  
21 /* Wire types. 0, 2 and 5 are what OTLP uses; 1 appears for fixed64. */
22 #define PO_PB_VARINT 0
23 #define PO_PB_FIXED64 1
24 #define PO_PB_BYTES 2
25 #define PO_PB_SGROUP 3 /* deprecated groups - see po_pbr_next */
26 #define PO_PB_EGROUP 4
27 #define PO_PB_FIXED32 5
28  
29 typedef struct {
30 const uint8_t *p;
31 const uint8_t *end;
32 int err; /* sticky: once set, every call fails */
33 } po_pbr;
34  
35 #define PO_PB_OK 0
36 #define PO_PB_ETRUNC 1 /* ran off the end of the buffer */
37 #define PO_PB_EVARINT 2 /* varint longer than ten bytes */
38 #define PO_PB_EWIRE 3 /* a wire type OTLP never uses */
39 #define PO_PB_EFIELD 4 /* field number 0, which is illegal */
40  
41 871 static void po_pbr_init(po_pbr *r, const void *buf, size_t len) {
42 871 r->p = (const uint8_t *)buf;
43 871 r->end = r->p + len;
44 871 r->err = PO_PB_OK;
45 871 }
46  
47 148 static int po_pbr_done(const po_pbr *r) { return r->p >= r->end; }
48  
49 /* A varint is at most ten bytes.
50 *
51 * The cap is not decoration. A corrupt or hostile stream can present an
52 * unbounded run of continuation bits, and a loop with no bound here is a hang
53 * on a malicious body - from an endpoint that is, by design, unauthenticated
54 * on a self-hosted install. The tenth byte may also only carry one meaningful
55 * bit; anything above bit 0 in it would overflow a uint64_t, so it is
56 * REFUSED rather than allowed to wrap. */
57 2990 static int po_pbr_varint(po_pbr *r, po_u64 *out) {
58 2990 po_u64 v = 0;
59 2990 int shift = 0, n = 0;
60  
61 2990 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 2990 2990
62 3166 ▶ while (r->p < r->end) {
Branch: 1 branch
BranchTrueFalseTotal
while (r->p < r->end) { 3164 2 3166
63 3164 uint8_t b = *r->p++;
64 3164 n++;
65 3164 ▶ if (n == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 10) { 9 3155 3164
66 /* Only bit 0 can survive a 63-bit shift. */
67 9 ▶ if (b > 1) { r->err = PO_PB_EVARINT; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (b > 1) { r->err = PO_PB_EVARINT; return 0; } 2 7 9
68 }
69 3162 v |= (po_u64)(b & 0x7F) << shift;
70 3162 ▶ if (!(b & 0x80)) { *out = v; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(b & 0x80)) { *out = v; return 1; } 2986 176 3162
71 176 ▶ if (n == 10) { r->err = PO_PB_EVARINT; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 10) { r->err = PO_PB_EVARINT; return 0; } 0 176 176
72 176 shift += 7;
73 }
74 2 r->err = PO_PB_ETRUNC;
75 2 return 0;
76 }
77  
78 /* A negative int32 is encoded SIGN-EXTENDED to ten bytes: -1 is
79 * ff ff ff ff ff ff ff ff ff 01. OTLP uses int32 for SeverityNumber,
80 * StatusCode and SpanKind, so a reader that assumes five bytes reads garbage,
81 * and one that reads the varint unsigned reports 4294967295 for -1. */
82 36 static int po_pbr_int32(po_pbr *r, int32_t *out) {
83 po_u64 v;
84 36 ▶ if (!po_pbr_varint(r, &v)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &v)) return 0; 0 36 36
85 36 *out = (int32_t)(uint32_t)(v & 0xFFFFFFFFu);
86 36 return 1;
87 }
88  
89 202 static int po_pbr_fixed64(po_pbr *r, po_u64 *out) {
90 po_u64 v;
91 202 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 202 202
92 202 ▶ if ((size_t)(r->end - r->p) < 8) { r->err = PO_PB_ETRUNC; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(r->end - r->p) < 8) { r->err = PO_PB_ETRUNC; return 0; } 0 202 202
93 202 memcpy(&v, r->p, 8); /* memcpy, not a cast: alignment */
94 202 r->p += 8;
95 202 *out = po_le64(v); /* protobuf fixed64 is little-endian */
96 202 return 1;
97 }
98  
99 2 static int po_pbr_fixed32(po_pbr *r, uint32_t *out) {
100 uint32_t v;
101 2 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 2 2
102 2 ▶ if ((size_t)(r->end - r->p) < 4) { r->err = PO_PB_ETRUNC; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(r->end - r->p) < 4) { r->err = PO_PB_ETRUNC; return 0; } 0 2 2
103 2 memcpy(&v, r->p, 4);
104 2 r->p += 4;
105 2 *out = po_le32(v);
106 2 return 1;
107 }
108  
109 85 static int po_pbr_double(po_pbr *r, double *out) {
110 po_u64 bits;
111 85 ▶ if (!po_pbr_fixed64(r, &bits)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed64(r, &bits)) return 0; 0 85 85
112 85 memcpy(out, &bits, 8); /* bit pattern, so NaN payloads survive */
113 85 return 1;
114 }
115  
116 0 static int po_pbr_float(po_pbr *r, float *out) {
117 uint32_t bits;
118 0 ▶ if (!po_pbr_fixed32(r, &bits)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_fixed32(r, &bits)) return 0; 0 0 0
119 0 memcpy(out, &bits, 4);
120 0 return 1;
121 }
122  
123 /* Borrows. The returned pointer is valid only while the caller's buffer is.
124 *
125 * A length-delimited field can claim a length past the end of the buffer, and
126 * the answer is an ERROR, never a clamp: clamping turns a truncated body into
127 * a shorter valid-looking one, which is a wrong answer rather than a refusal. */
128 1210 static int po_pbr_bytes(po_pbr *r, const uint8_t **p, size_t *len) {
129 po_u64 n;
130 1210 ▶ if (!po_pbr_varint(r, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &n)) return 0; 1 1209 1210
131 1209 ▶ if (n > (po_u64)(r->end - r->p)) { r->err = PO_PB_ETRUNC; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > (po_u64)(r->end - r->p)) { r->err = PO_PB_ETRUNC; return 0; } 94 1115 1209
132 1115 *p = r->p;
133 1115 *len = (size_t)n;
134 1115 r->p += (size_t)n;
135 1115 return 1;
136 }
137  
138 /* A nested message, read in place with no allocation. */
139 534 static int po_pbr_sub(po_pbr *r, po_pbr *sub) {
140 const uint8_t *p;
141 size_t len;
142 534 ▶ if (!po_pbr_bytes(r, &p, &len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &len)) return 0; 0 534 534
143 534 po_pbr_init(sub, p, len);
144 534 return 1;
145 }
146  
147 /* Advance past a field of the given wire type without interpreting it.
148 *
149 * Skipping unknown fields is the compatibility contract, not a convenience.
150 * A reader that errors on a field it does not recognise breaks on the OTLP
151 * release AFTER the one it was written for, and it breaks by rejecting data
152 * rather than by ignoring an addition. */
153 304 static int po_pbr_skip(po_pbr *r, uint32_t wire) {
154 po_u64 v;
155 const uint8_t *p;
156 size_t len;
157 304 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 304 304
158 304 switch (wire) {
159 39 case PO_PB_VARINT: return po_pbr_varint(r, &v);
160 2 case PO_PB_FIXED64: return po_pbr_fixed64(r, &v);
161 0 case PO_PB_FIXED32: { uint32_t f; return po_pbr_fixed32(r, &f); }
162 262 case PO_PB_BYTES: return po_pbr_bytes(r, &p, &len);
163 1 default: r->err = PO_PB_EWIRE; return 0;
164 }
165 }
166  
167 /* The iterator. Returns 1 on a field, 0 at the end of the buffer or on error
168 * (check r->err to tell them apart).
169 *
170 * Wire types 3 and 4 are the deprecated group encodings. OTLP does not use
171 * them and they carry no length prefix, so a reader cannot skip one - it
172 * would have to understand the schema to find the matching end tag. Their
173 * presence means the bytes are not OTLP, so the message is refused rather
174 * than half-read. */
175 2269 static int po_pbr_next(po_pbr *r, uint32_t *field, uint32_t *wire) {
176 po_u64 tag;
177 2269 ▶ if (r->err || r->p >= r->end) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->err || r->p >= r->end) return 0; 2269 0 2269
if (r->err || r->p >= r->end) return 0; 725 1544 2269
178 1544 ▶ if (!po_pbr_varint(r, &tag)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &tag)) return 0; 0 1544 1544
179 1544 *field = (uint32_t)(tag >> 3);
180 1544 *wire = (uint32_t)(tag & 7);
181 1544 ▶ if (*field == 0) { r->err = PO_PB_EFIELD; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (*field == 0) { r->err = PO_PB_EFIELD; return 0; } 1 1543 1544
182 1543 ▶ if (*wire == PO_PB_SGROUP || *wire == PO_PB_EGROUP) {
Branch: 2 branches
BranchTrueFalseTotal
if (*wire == PO_PB_SGROUP || *wire == PO_PB_EGROUP) { 1541 2 1543
if (*wire == PO_PB_SGROUP || *wire == PO_PB_EGROUP) { 1 1540 1541
183 3 r->err = PO_PB_EWIRE;
184 3 return 0;
185 }
186 1540 return 1;
187 }
188  
189 /* Packed repeated numerics.
190 *
191 * A repeated numeric field may arrive packed (one length-delimited blob) or
192 * unpacked (one tag per value), and BOTH must be accepted always. otel_pb.h
193 * writes packed; a conforming older encoder may not, and a reader that
194 * handles only packed gets empty histogram buckets from valid input.
195 *
196 * The caller loops: begin, then repeated step calls.
197 */
198 typedef struct { po_pbr inner; int packed; } po_pb_packed;
199  
200 36 static int po_pb_packed_begin(po_pbr *r, uint32_t wire, po_pb_packed *it) {
201 36 ▶ if (wire == PO_PB_BYTES) { /* packed */
Branch: 1 branch
BranchTrueFalseTotal
if (wire == PO_PB_BYTES) { /* packed */ 36 0 36
202 36 it->packed = 1;
203 36 return po_pbr_sub(r, &it->inner);
204 }
205 0 it->packed = 0; /* unpacked: this tag is one value */
206 0 return 1;
207 }
208  
209 83 static int po_pb_packed_varint(po_pbr *r, po_pb_packed *it, po_u64 *out) {
210 83 ▶ if (it->packed) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->packed) { 83 0 83
211 83 ▶ if (po_pbr_done(&it->inner) || it->inner.err) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (po_pbr_done(&it->inner) || it->inner.err) return 0; 65 18 83
if (po_pbr_done(&it->inner) || it->inner.err) return 0; 0 65 65
212 65 return po_pbr_varint(&it->inner, out);
213 }
214 0 return po_pbr_varint(r, out);
215 }
216  
217 65 static int po_pb_packed_double(po_pbr *r, po_pb_packed *it, double *out) {
218 65 ▶ if (it->packed) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->packed) { 65 0 65
219 65 ▶ if (po_pbr_done(&it->inner) || it->inner.err) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (po_pbr_done(&it->inner) || it->inner.err) return 0; 47 18 65
if (po_pbr_done(&it->inner) || it->inner.err) return 0; 0 47 47
220 47 return po_pbr_double(&it->inner, out);
221 }
222 0 return po_pbr_double(r, out);
223 }
224  
225 /* Propagate a sub-reader's error to its parent, so a fault deep in a nested
226 * message is not lost when the sub goes out of scope. */
227 302 static void po_pbr_join(po_pbr *parent, const po_pbr *sub) {
228 302 ▶ if (!parent->err && sub->err) parent->err = sub->err;
Branch: 2 branches
BranchTrueFalseTotal
if (!parent->err && sub->err) parent->err = sub->err; 302 0 302
if (!parent->err && sub->err) parent->err = sub->err; 0 302 302
229 302 }
230  
231 129 static const char *po_pbr_errstr(int err) {
232 129 switch (err) {
233 28 case PO_PB_OK: return "ok";
234 96 case PO_PB_ETRUNC: return "truncated";
235 2 case PO_PB_EVARINT: return "varint too long";
236 2 case PO_PB_EWIRE: return "unsupported wire type";
237 1 case PO_PB_EFIELD: return "field number 0";
238 0 default: return "unknown";
239 }
240 }
241  
242 #endif /* PO_PB_READ_H */
« include/punk_observe/po_expr.h ↑ Summary include/punk_observe/po_seg.h »