Skip to content

Commit

Permalink
fix: Add check for lower bound with forceLong=number (#1057)
Browse files Browse the repository at this point in the history
With default plugin options, or `useJsTypeOverride` for a field with
option `jstype = JS_NUMBER`, 64-bit integral values are converted to a
JavaScript Number when decoding from binary. When a value is too large
for Number, an error is thrown.

But signed integers can also be too _small_ for Number. This PR adds a
check for the lower bound.
  • Loading branch information
timostamm committed Jun 7, 2024
1 parent 16195d0 commit 01ef3c3
Show file tree
Hide file tree
Showing 70 changed files with 210 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/bytes-node/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/emit-default-values-json/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/fieldoption-jstype/fieldoption-jstype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/file-suffix/google/protobuf/timestamp.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/grpc-js/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/grpc-js/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/grpc-web/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/import-mapping/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/import-suffix/google/protobuf/timestamp.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/map-long-optional/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/meta-typings/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/meta-typings/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/meta-typings/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/nice-grpc/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/nice-grpc/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/options/google/protobuf/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5881,6 +5881,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/output-decode-only/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/proto2-no-default-vals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/proto2-no-optionals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/proto2/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-esmodule-interop/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-json-name/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-long/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-optionals/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-optionals/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-optionals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-prototype-defaults/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
3 changes: 3 additions & 0 deletions integration/simple-snake/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function longToNumber(long: Long): number {
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) {
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
}
if (long.lt(globalThis.Number.MIN_SAFE_INTEGER)) {
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
}
return long.toNumber();
}

Expand Down
Loading

0 comments on commit 01ef3c3

Please sign in to comment.