question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StringStream is wrongly assuming that array items cannot be empty

See original GitHub issue

When trying to deserialized a serialized array with an empty item, neon-js is throwing an error:

Reached the end of the stream!

I believe this should not happen, it should not be illegal to have empty items in a serialized array.

For example:

// We have a serialized array from a smart contract, for example: ['test','neon','']
serialized = '800200077075626c69736880040014f8af73dc ...'

// We try to deserialize it in neon-js:
deserialized = Neon.sc.StackItem.deserialize(serialized).value;
console.log(deserialized);

This will throw an error:

(node:25781) UnhandledPromiseRejectionWarning: Error: Reached the end of the stream!
    at StringStream.read (/usr/local/src/project/node_modules/@cityofzion/neon-js/dist/index.js:24755:19)
    at Function._deserialize (/usr/local/src/project/node_modules/@cityofzion/neon-js/dist/index.js:23029:33)
    at Function._deserialize (/usr/local/src/project/node_modules/@cityofzion/neon-js/dist/index.js:23014:42)
    at Function._deserialize (/usr/local/src/project/node_modules/@cityofzion/neon-js/dist/index.js:23014:42)
    at Function.deserialize (/usr/local/src/project/node_modules/@cityofzion/neon-js/dist/index.js:23005:21)
    at init (/usr/local/src/project/index.js:62:35)
    at Object.<anonymous> (/usr/local/src/project/index.js:71:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
(node:25781) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25781) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Proposed fix:

diff --git a/packages/neon-core/src/u/StringStream.ts b/packages/neon-core/src/u/StringStream.ts
index ebca4f2..f577213 100644
--- a/packages/neon-core/src/u/StringStream.ts
+++ b/packages/neon-core/src/u/StringStream.ts
@@ -51,9 +51,6 @@ export class StringStream {
    * ss.read(2); // "0203"
    */
   public read(bytes: number = 1): string {
-    if (this.isEmpty()) {
-      throw new Error("Reached the end of the stream!");
-    }
     const out = this.str.substr(this.pter, bytes * 2);
     this.pter += bytes * 2;
     return out;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
snowypowerscommented, Apr 2, 2019

v4.4.0 is released and includes this fix. Please verify it on your side.

1reaction
snowypowerscommented, Mar 2, 2019

I dug into this and found the reason why. When you place the zero at the end, the serialized output actually serialized it as a null, the OpCodes produced are 0000 which can be interpreted as:

00 (OpCode to indicate type ByteArray)
00 (OpCode to indicate length of bytes to read)

Essentially, the read(0) bytes off the stream is triggering the error because the stream is empty. The issue I have here is if this is intended. Are you intending to insert a null or actually insert the bytearray of length 1 with data 00. Because you used an int instead of a string there, the case is even more ambiguous.

If the null case is the correct way, we can amend read(0) to return null. Else, we should be amending the StackItem deserializer to prevent a read(0) and fill in some defaults.

Read more comments on GitHub >

github_iconTop Results From Across the Web

stringstream, string, and char* conversion confusion
The assumption that stringstream.str().c_str() could be assigned to a const char* led to a bug that took me a while to track down....
Read more >
Please add a Pretty-Print option for arrays to stay always in ...
Please add a Pretty-Printing option for arrays to stay always in one line (don't add lines) if dump() parameter > -1 or std::setw())...
Read more >
Stringstream Consectutive Whitespace - C++ Forum
I need that space to be caught in the steam, there will always be only one space after a various first word, but...
Read more >
[C++]Error when moving char array to stringstream
I am trying to turn part of an array of char into a string(ie, char a[100] to a[700] out of a[2000] to coolString)...
Read more >
Error messages
The type of identifier cannot be deduced. Typically, an undeclared object is assigned an empty set. E-157 Scalar expression expected. Examples: declarations B ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found