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.
@@ -19,6 +19,9 @@ typedef struct {
19
19
  size_t limit;
20
20
  size_t pos;
21
21
  pbw_status status;
22
+ /* Set when a malloc for the deterministic map sort failed, so a caller
23
+ * buffer that also overflowed can report the right one. */
24
+ int alloc_failed;
22
25
  } writer;
23
26
 
24
27
  int
@@ -115,6 +118,7 @@ w_init(writer *w, pbw_buf *buf)
115
118
  w->limit = buf->cap;
116
119
  w->pos = 0;
117
120
  w->status = PBW_OK;
121
+ w->alloc_failed = 0;
118
122
  }
119
123
 
120
124
  PBW_INLINE int
@@ -170,9 +174,9 @@ w_varint(writer *w, uint64_t value)
170
174
  {
171
175
  uint8_t *out;
172
176
 
173
- /* Reserve the maximum width so the value is written in place: encoding only
174
- * appends, so the slack past w->pos is never read. */
175
- if (!w_reserve(w, 10)) {
177
+ /* Reserve exactly what the value needs: a caller buffer sized by
178
+ * pbw_message_encoded_size() must accept the write with no slack. */
179
+ if (!w_reserve(w, pbw_varint_size(value))) {
176
180
  return;
177
181
  }
178
182
  out = w->base + w->pos;
@@ -219,9 +223,10 @@ w_tag_word_le(writer *w, uint32_t word, size_t len)
219
223
  #elif defined(_MSC_VER) || \
220
224
  (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
221
225
  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
222
- /* memcpy of a fixed size compiles to one store and does not assume the
223
- * output offset is aligned. */
224
- memcpy(w->base + w->pos, &word, sizeof(word));
226
+ /* memcpy of the exact length: the word holds the tag little-endian, so
227
+ * copying only `len` bytes stores exactly the tag. A fixed-size 4-byte
228
+ * store would write past a caller buffer that fits exactly. */
229
+ memcpy(w->base + w->pos, &word, len);
225
230
  #else
226
231
  /* Unknown byte order: the portable spelling. */
227
232
  uint8_t *out = w->base + w->pos;
@@ -234,21 +239,20 @@ w_tag_word_le(writer *w, uint32_t word, size_t len)
234
239
  w->pos += len;
235
240
  }
236
241
 
237
- /* Writes a precomputed tag. Tags are at most five bytes and the buffer is
238
- * reserved for five, so the four-byte word can be stored unconditionally and
239
- * the slack is overwritten by the next write. */
242
+ /* Writes a precomputed tag. Reserves exactly `len` bytes, so a caller buffer
243
+ * sized by pbw_message_encoded_size() fits with no slack. */
240
244
  PBW_INLINE void
241
245
  w_tag(writer *w, uint32_t word, size_t len)
242
246
  {
243
- if (!w_reserve(w, 5)) {
247
+ if (!w_reserve(w, len)) {
244
248
  return;
245
249
  }
246
250
  w_tag_word_le(w, word, len);
247
251
  }
248
252
 
249
253
  /* Same writes without a capacity check, for callers that reserved once for
250
- * the whole field (tag plus value). Encoding only appends, so the reserved
251
- * slack past w->pos is never read. */
254
+ * the whole field (tag plus value). The tag write stores exactly `len`
255
+ * bytes. */
252
256
  PBW_INLINE void
253
257
  w_tag_noreserve(writer *w, uint32_t word, size_t len)
254
258
  {
@@ -447,12 +451,16 @@ write_group_body(writer *w, const pbw_field *field, const pbw_value *value,
447
451
  if (w->status != PBW_OK) {
448
452
  return;
449
453
  }
450
- /* Format the END tag straight into the output: bouncing it through a
451
- * stack temp trips -Werror=array-bounds under _FORTIFY_SOURCE once
452
- * w_copy inlines (a runtime length against a 5-byte object), and the
453
- * extra copy is pointless anyway. */
454
- if (!w_reserve(w, 5)) {
455
- return;
454
+ /* Format the END tag straight into the output: reserve exactly what the
455
+ * tag needs so a caller buffer sized by pbw_message_encoded_size()
456
+ * fits with no slack. */
457
+ {
458
+ size_t need = pbw_varint_size(((uint64_t)field->number << 3) |
459
+ PBW_WIRE_END_GROUP);
460
+
461
+ if (!w_reserve(w, need)) {
462
+ return;
463
+ }
456
464
  }
457
465
  w->pos += pbw_varint_write(w->base + w->pos,
458
466
  ((uint64_t)field->number << 3) |
@@ -519,19 +527,19 @@ write_element(writer *w, const pbw_field *field, const pbw_value *value,
519
527
  return;
520
528
  }
521
529
  /* Length is known before the payload: reserve once for tag,
522
- * length and payload instead of three separate checks. The tag
523
- * write stores a whole word, so reserve 4 for it. */
530
+ * length and payload instead of three separate checks. The reserve
531
+ * is exact, so a caller buffer sized by pbw_message_encoded_size()
532
+ * fits with no slack. */
524
533
  size_t lb = pbw_varint_size(value->as.bytes.len);
525
534
  if (field->tag_len <= 4) {
526
- if (value->as.bytes.len > SIZE_MAX - 4 - lb) {
535
+ if (value->as.bytes.len > SIZE_MAX - field->tag_len - lb) {
527
536
  /* A payload that big cannot exist (it would need SIZE_MAX
528
537
  * bytes of live memory); refuse rather than wrap the sum into
529
538
  * a tiny reserve and then copy past it. */
530
539
  w->status = PBW_ERR_LIMIT;
531
540
  return;
532
541
  }
533
- if (!w_reserve(w, 4 + lb + value->as.bytes.len)) {
534
- w->status = PBW_ERR_NOMEM;
542
+ if (!w_reserve(w, field->tag_len + lb + value->as.bytes.len)) {
535
543
  return;
536
544
  }
537
545
  w_tag_noreserve(w, field->tag_word, field->tag_len);
@@ -575,11 +583,10 @@ write_element(writer *w, const pbw_field *field, const pbw_value *value,
575
583
  return;
576
584
  }
577
585
  /* Tag plus length placeholder in one reserve; the payload that
578
- * follows grows on demand like before. The tag write stores a
579
- * whole word, so reserve 4 for it. */
586
+ * follows grows on demand like before. The reserve is exact, so a
587
+ * caller buffer sized by pbw_message_encoded_size() fits. */
580
588
  if (field->tag_len <= 4) {
581
- if (!w_reserve(w, 4 + 1)) {
582
- w->status = PBW_ERR_NOMEM;
589
+ if (!w_reserve(w, field->tag_len + 1)) {
583
590
  return;
584
591
  }
585
592
  w_tag_noreserve(w, field->tag_word, field->tag_len);
@@ -608,11 +615,10 @@ write_element(writer *w, const pbw_field *field, const pbw_value *value,
608
615
  return;
609
616
  }
610
617
  /* One reserve for tag plus value: the common small-message path
611
- * then writes without another capacity check. The tag write stores
612
- * a whole word, so reserve 4 for it. */
618
+ * then writes without another capacity check. The reserve is exact,
619
+ * so a caller buffer sized by pbw_message_encoded_size() fits. */
613
620
  if (field->tag_len <= 4) {
614
- if (!w_reserve(w, 4 + 4)) {
615
- w->status = PBW_ERR_NOMEM;
621
+ if (!w_reserve(w, field->tag_len + 4)) {
616
622
  return;
617
623
  }
618
624
  w_tag_noreserve(w, field->tag_word, field->tag_len);
@@ -630,8 +636,7 @@ write_element(writer *w, const pbw_field *field, const pbw_value *value,
630
636
  return;
631
637
  }
632
638
  if (field->tag_len <= 4) {
633
- if (!w_reserve(w, 4 + 8)) {
634
- w->status = PBW_ERR_NOMEM;
639
+ if (!w_reserve(w, field->tag_len + 8)) {
635
640
  return;
636
641
  }
637
642
  w_tag_noreserve(w, field->tag_word, field->tag_len);
@@ -647,12 +652,14 @@ write_element(writer *w, const pbw_field *field, const pbw_value *value,
647
652
  return;
648
653
  }
649
654
  if (field->tag_len <= 4) {
650
- if (!w_reserve(w, 4 + 10)) {
651
- w->status = PBW_ERR_NOMEM;
655
+ uint64_t v = wire_value(field->type, value->as.u64);
656
+ size_t vs = pbw_varint_size(v);
657
+
658
+ if (!w_reserve(w, field->tag_len + vs)) {
652
659
  return;
653
660
  }
654
661
  w_tag_noreserve(w, field->tag_word, field->tag_len);
655
- w_varint_noreserve(w, wire_value(field->type, value->as.u64));
662
+ w_varint_noreserve(w, v);
656
663
  return;
657
664
  }
658
665
  w_field_tag(w, field, 0);
@@ -740,9 +747,10 @@ write_packed(writer *w, const pbw_field *field, const pbw_value *list)
740
747
  case PBW_TYPE_BOOL: {
741
748
  uint8_t *out;
742
749
  payload = n;
743
- if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
744
- pbw_varint_size(payload) + payload)) {
745
- w->status = PBW_ERR_NOMEM;
750
+ /* Exact reserve: tag plus length prefix plus payload, so a caller
751
+ * buffer sized by pbw_message_encoded_size() fits with no slack. */
752
+ if (!w_reserve(w, field->len_tag_len + pbw_varint_size(payload) +
753
+ payload)) {
746
754
  return;
747
755
  }
748
756
  if (field->len_tag_len <= 4) {
@@ -770,9 +778,9 @@ write_packed(writer *w, const pbw_field *field, const pbw_value *list)
770
778
  case PBW_TYPE_SFIXED32: {
771
779
  uint8_t *out;
772
780
  payload = n * 4;
773
- if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
774
- pbw_varint_size(payload) + payload)) {
775
- w->status = PBW_ERR_NOMEM;
781
+ /* Exact reserve, like the bool case above. */
782
+ if (!w_reserve(w, field->len_tag_len + pbw_varint_size(payload) +
783
+ payload)) {
776
784
  return;
777
785
  }
778
786
  if (field->len_tag_len <= 4) {
@@ -805,9 +813,9 @@ write_packed(writer *w, const pbw_field *field, const pbw_value *list)
805
813
  case PBW_TYPE_SFIXED64: {
806
814
  uint8_t *out;
807
815
  payload = n * 8;
808
- if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
809
- pbw_varint_size(payload) + payload)) {
810
- w->status = PBW_ERR_NOMEM;
816
+ /* Exact reserve, like the bool case above. */
817
+ if (!w_reserve(w, field->len_tag_len + pbw_varint_size(payload) +
818
+ payload)) {
811
819
  return;
812
820
  }
813
821
  if (field->len_tag_len <= 4) {
@@ -889,11 +897,11 @@ write_packed(writer *w, const pbw_field *field, const pbw_value *list)
889
897
  }
890
898
  break;
891
899
  }
892
- /* Four bytes cover the tag word store; a five-byte length tag (field
893
- * number >= 2^21) is copied byte by byte instead. */
894
- if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
895
- pbw_varint_size(payload) + payload)) {
896
- w->status = PBW_ERR_NOMEM;
900
+ /* Exact reserve for tag plus length prefix plus payload: a caller
901
+ * buffer sized by pbw_message_encoded_size() fits with no slack, also
902
+ * for five-byte length tags (field number >= 2^21). */
903
+ if (!w_reserve(w, field->len_tag_len + pbw_varint_size(payload) +
904
+ payload)) {
897
905
  return;
898
906
  }
899
907
  if (field->len_tag_len <= 4) {
@@ -965,41 +973,66 @@ map_key_cmp(const pbw_field *kf, const pbw_value *a, const pbw_value *b)
965
973
  if (ka == kb) {
966
974
  return 0;
967
975
  }
968
- if (ka == NULL) {
969
- return -1;
970
- }
971
- if (kb == NULL) {
972
- return 1;
973
- }
974
- if (ka->kind == PBW_VALUE_BYTES && kb->kind == PBW_VALUE_BYTES) {
975
- size_t n = ka->as.bytes.len < kb->as.bytes.len ? ka->as.bytes.len
976
- : kb->as.bytes.len;
976
+ /* An absent key is its field default (0, or the empty string): the
977
+ * reference sorts by the logical key, not by presence. */
978
+ if (kf->type == PBW_TYPE_STRING) {
979
+ size_t la = ka != NULL ? ka->as.bytes.len : 0;
980
+ size_t lb = kb != NULL ? kb->as.bytes.len : 0;
981
+ size_t n = la < lb ? la : lb;
977
982
  int r = n > 0 ? memcmp(ka->as.bytes.data, kb->as.bytes.data, n) : 0;
978
983
 
979
984
  if (r != 0) {
980
985
  return r < 0 ? -1 : 1;
981
986
  }
982
- if (ka->as.bytes.len != kb->as.bytes.len) {
983
- return ka->as.bytes.len < kb->as.bytes.len ? -1 : 1;
987
+ if (la != lb) {
988
+ return la < lb ? -1 : 1;
984
989
  }
985
990
  return 0;
986
991
  }
987
- switch (kf->type) {
988
- case PBW_TYPE_INT32:
989
- case PBW_TYPE_INT64:
990
- case PBW_TYPE_SINT32:
991
- case PBW_TYPE_SINT64:
992
- case PBW_TYPE_SFIXED32:
993
- case PBW_TYPE_SFIXED64:
994
- if ((int64_t)ka->as.u64 != (int64_t)kb->as.u64) {
995
- return (int64_t)ka->as.u64 < (int64_t)kb->as.u64 ? -1 : 1;
992
+ {
993
+ uint64_t ua = ka != NULL ? ka->as.u64 : 0;
994
+ uint64_t ub = kb != NULL ? kb->as.u64 : 0;
995
+
996
+ switch (kf->type) {
997
+ /* Normalize to the width the wire carries: the encoder truncates
998
+ * INT32/UINT32/BOOL, so the sort must compare the same bits. */
999
+ case PBW_TYPE_INT32:
1000
+ case PBW_TYPE_SINT32:
1001
+ case PBW_TYPE_SFIXED32: {
1002
+ int64_t sa = (int64_t)(int32_t)(uint32_t)ua;
1003
+ int64_t sb = (int64_t)(int32_t)(uint32_t)ub;
1004
+
1005
+ return sa < sb ? -1 : sa > sb ? 1
1006
+ : 0;
996
1007
  }
997
- return 0;
998
- default:
999
- if (ka->as.u64 != kb->as.u64) {
1000
- return ka->as.u64 < kb->as.u64 ? -1 : 1;
1008
+ case PBW_TYPE_INT64:
1009
+ case PBW_TYPE_SINT64:
1010
+ case PBW_TYPE_SFIXED64: {
1011
+ int64_t sa = (int64_t)ua;
1012
+ int64_t sb = (int64_t)ub;
1013
+
1014
+ return sa < sb ? -1 : sa > sb ? 1
1015
+ : 0;
1016
+ }
1017
+ case PBW_TYPE_BOOL: {
1018
+ uint64_t na = ua != 0 ? 1 : 0;
1019
+ uint64_t nb = ub != 0 ? 1 : 0;
1020
+
1021
+ return na < nb ? -1 : na > nb ? 1
1022
+ : 0;
1023
+ }
1024
+ case PBW_TYPE_UINT32:
1025
+ case PBW_TYPE_FIXED32: {
1026
+ uint32_t na = (uint32_t)ua;
1027
+ uint32_t nb = (uint32_t)ub;
1028
+
1029
+ return na < nb ? -1 : na > nb ? 1
1030
+ : 0;
1031
+ }
1032
+ default:
1033
+ return ua < ub ? -1 : ua > ub ? 1
1034
+ : 0;
1001
1035
  }
1002
- return 0;
1003
1036
  }
1004
1037
  }
1005
1038
 
@@ -1063,6 +1096,7 @@ write_map_sorted(writer *w, const pbw_field *field, const pbw_value *list,
1063
1096
  }
1064
1097
  if (n > SIZE_MAX / (2 * sizeof(*items))) {
1065
1098
  w->status = PBW_ERR_NOMEM;
1099
+ w->alloc_failed = 1;
1066
1100
  return;
1067
1101
  }
1068
1102
  /* One allocation split in two: halves the malloc/free calls on the
@@ -1070,6 +1104,7 @@ write_map_sorted(writer *w, const pbw_field *field, const pbw_value *list,
1070
1104
  items = (pbw_value **)malloc(2 * n * sizeof(*items));
1071
1105
  if (items == NULL) {
1072
1106
  w->status = PBW_ERR_NOMEM;
1107
+ w->alloc_failed = 1;
1073
1108
  return;
1074
1109
  }
1075
1110
  tmp = items + n;
@@ -1091,8 +1126,13 @@ write_map_sorted(writer *w, const pbw_field *field, const pbw_value *list,
1091
1126
  em = items[j]->as.msg;
1092
1127
  k = pbw_message_get(em, 1);
1093
1128
 
1094
- if (k != NULL && k->kind == PBW_VALUE_BYTES &&
1095
- !pbw_bytes_readable(k)) {
1129
+ /* The key must be the kind the comparator reads: a string key is
1130
+ * compared as bytes (reading kernel memory through a UINT value's
1131
+ * bits would follow a NULL data pointer), an integer key as bits. */
1132
+ if (k != NULL &&
1133
+ ((kf->type == PBW_TYPE_STRING &&
1134
+ (k->kind != PBW_VALUE_BYTES || !pbw_bytes_readable(k))) ||
1135
+ (kf->type != PBW_TYPE_STRING && k->kind != PBW_VALUE_UINT))) {
1096
1136
  free(items);
1097
1137
  w->status = PBW_ERR_INVALID;
1098
1138
  return;
@@ -1497,8 +1537,9 @@ encode_alloc(const pbw_message *msg, int det, uint8_t **out, size_t *len)
1497
1537
  pbw_buf_init(&buf);
1498
1538
  {
1499
1539
  /* Load-time hint first: a decoded tree re-encodes to about its wire
1500
- * length, so no walk is needed (exact for untouched trees; the
1501
- * writer grows if it was mutated, hence no invalidation).
1540
+ * length, so no walk is needed (exact for untouched trees;
1541
+ * mutation zeroes the hint, and the writer grows when it
1542
+ * underestimates).
1502
1543
  * Hand-built trees start from a small fixed buffer and grow on
1503
1544
  * demand: walking the tree to size it costs more than one or two
1504
1545
  * doublings for small messages, and large ones only pay a few
@@ -1582,8 +1623,8 @@ encode_into(const pbw_message *msg, int det, void *out, size_t cap, size_t *len)
1582
1623
  w_init(&w, &buf);
1583
1624
  encode_message(&w, msg, 0, det);
1584
1625
  status = w.status;
1585
- if (status == PBW_ERR_NOMEM && buf.fixed) {
1586
- /* A fixed buffer cannot fail to allocate: the write did not fit. */
1626
+ if (status == PBW_ERR_NOMEM && buf.fixed && !w.alloc_failed) {
1627
+ /* A caller buffer does not allocate: the write did not fit. */
1587
1628
  status = PBW_ERR_LIMIT;
1588
1629
  }
1589
1630
  if (status == PBW_OK) {