JS: Handling Infinity values for Float fields

Open
#49 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript
Domain
backend

Research direction

Start in encoder.js at BinaryEncoder.prototype.writeFloat and inspect the linked implementation, then trace how splitFloat32 and writeUint32 handle Infinity. Done means the accepted behavior for Infinity is implemented consistently with the method comment and clearly documented.

Written by the indexing model from the issue text.

Description

bug javascript triaged

Hello!

I noticed that in encoder.js, the method does not allow writeFloat does not accept Infinity as its value

https://github.com/protocolbuffers/protobuf/blob/d9ccd0c0e6bbda9bf4476088eeb46b02d7dcd327/js/binary/encoder.js#L365-L375

/**
 * Writes a single-precision floating point value to the buffer. Numbers
 * requiring more than 32 bits of precision will be truncated.
 * @param {number} value The value to write.
 */
jspb.BinaryEncoder.prototype.writeFloat = function(value) {
  goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) &&
                      (value <= jspb.BinaryConstants.FLOAT32_MAX));
  jspb.utils.splitFloat32(value);
  this.writeUint32(jspb.utils.split64Low);
};

And its comment seems misleading because it says Numbers requiring more than 32 bits of precision will be truncated. but in reality, it throws an error.

Shouldn't writeFloat remove this restriction or at least this assert?

Thanks in advance,

Dominant language
JavaScript
Stars
471
Forks
92
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from protocolbuffers/protobuf-javascript

All issues in protocolbuffers/protobuf-javascript

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.