Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Suspecting a bug in checking binary fields length #141

Open
kugesh-Rajasekaran opened this issue May 25, 2024 · 4 comments
Open

BUG: Suspecting a bug in checking binary fields length #141

kugesh-Rajasekaran opened this issue May 25, 2024 · 4 comments

Comments

@kugesh-Rajasekaran
Copy link

kugesh-Rajasekaran commented May 25, 2024

When we check the length of a binary field, we compare the binary length directly with the length of the incoming value, which is in hex.

   if (this_format.ContentType === 'b') {
              if (this_format.MaxLen  === this.Msg[field].length) {    // this.Msg[field] is in hex format and this_format.MaxLen is in bytes
                const size = this_format.MaxLen / 2;
                const thisBuff = Buffer.alloc(size, this.Msg[field], 'hex');
                buff = Buffer.concat([buff, thisBuff]);
    ...

This should be

   if (this_format.ContentType === 'b') {
              if (this_format.MaxLen * 2  === this.Msg[field].length) {    // multiplied with 2 to make a byte with couple of hex characters 
                const size = this_format.MaxLen / 2;
                const thisBuff = Buffer.alloc(size, this.Msg[field], 'hex');
                buff = Buffer.concat([buff, thisBuff]);
    ...

Let me know if any further details necessary

@zemuldo
Copy link
Owner

zemuldo commented May 26, 2024

Please explain the difference in the code here for context :)

@kugesh-Rajasekaran
Copy link
Author

// assemble0_127_Fields.ts, line 56
if (this_format.ContentType === 'b') {
  if (this_format.MaxLen === this.Msg[field].length) {
    ...
  }
}

this.Msg[field] - string in hex format
this_format.MaxLen - length in bytes

In this code, each byte is formed by combining two characters of hex. However, I believe the comparison this_format.MaxLen === this.Msg[field].length is incorrect.
For example, if this_format.MaxLen is 8 (representing 8 bytes), the input hex value should be 16 characters long (2 characters per byte), but the current comparison expects it to be 8 characters long.
I propose the following modification to the code:

if (this_format.ContentType === 'b') {
  if (this_format.MaxLen * 2 === this.Msg[field].length) {     // multiplied by 2 to make a byte with a pair of hex characters
    ...
  }
}

Could you please check this and let me know if it is correct? If it looks good, I will proceed with raising a pull request.

@kugesh-Rajasekaran
Copy link
Author

Any updates on this?

@zemuldo
Copy link
Owner

zemuldo commented Jun 7, 2024

I see your point. While I investigate, Happy to review a PR, Lets see how it works with the tests.
I will be looking in to this next week :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants