compact-encoding 3.1.0 → 3.3.0

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.
Files changed (2) hide show
  1. package/index.js +24 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -171,7 +171,9 @@ const uint56 = (exports.uint56 = {
171
171
  },
172
172
  decode(state) {
173
173
  if (state.end - state.start < 7) throw new Error('Out of bounds')
174
- return uint24.decode(state) + 0x1000000 * uint32.decode(state)
174
+ return validateSafeUint(
175
+ uint24.decode(state) + 0x1000000 * uint32.decode(state)
176
+ )
175
177
  }
176
178
  })
177
179
 
@@ -187,7 +189,9 @@ const uint64 = (exports.uint64 = {
187
189
  },
188
190
  decode(state) {
189
191
  if (state.end - state.start < 8) throw new Error('Out of bounds')
190
- return uint32.decode(state) + 0x100000000 * uint32.decode(state)
192
+ return validateSafeUint(
193
+ uint32.decode(state) + 0x100000000 * uint32.decode(state)
194
+ )
191
195
  }
192
196
  })
193
197
 
@@ -203,7 +207,9 @@ exports.uint64be = {
203
207
  },
204
208
  decode(state) {
205
209
  if (state.end - state.start < 8) throw new Error('Out of bounds')
206
- return 0x100000000 * uint32be.decode(state) + uint32be.decode(state)
210
+ return validateSafeUint(
211
+ 0x100000000 * uint32be.decode(state) + uint32be.decode(state)
212
+ )
207
213
  }
208
214
  }
209
215
 
@@ -1085,7 +1091,22 @@ function zigZagEncodeBigInt(n) {
1085
1091
  return n < 0n ? 2n * -n - 1n : n === 0n ? 0n : 2n * n
1086
1092
  }
1087
1093
 
1094
+ function validateSafeUint(n) {
1095
+ if (n > Number.MAX_SAFE_INTEGER)
1096
+ throw new Error(
1097
+ 'uint is greater than the maximum safe integer, use biguint/bigint'
1098
+ )
1099
+ return n
1100
+ }
1101
+
1088
1102
  function validateUint(n) {
1089
1103
  if (n >= 0 === false /* Handles NaN as well */)
1090
1104
  throw new Error('uint must be positive')
1105
+ // Beyond this, Number arithmetic loses precision and silently corrupts the
1106
+ // encoding. The zig-zag int codecs route through here too, so this also
1107
+ // guards int values whose doubled magnitude exceeds the safe range.
1108
+ if (n > Number.MAX_SAFE_INTEGER)
1109
+ throw new Error(
1110
+ 'integer is greater than the maximum safe integer, use biguint/bigint'
1111
+ )
1091
1112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
6
  "files": [