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

Fixed not allowing empty length user data #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pvyleta
Copy link
Collaborator

@pvyleta pvyleta commented Nov 12, 2019

Current implementation is not allowing zero-lenght empty data, which is by no means forbiddedn by the standard.

Current implementation is not allowing zero-lenght empty data, which is by no means forbiddedn by the standard.
@KVAnton-WEB
Copy link

KVAnton-WEB commented Jan 3, 2020

Current implementation is not allowing zero-lenght empty data, which is by no means forbiddedn by the standard.
if( usAdditionalLen + 2 <= MB_FUNC_OTHER_REP_SLAVEID_BUF )

Are you seriously? it's a bomb)

  1. eStatus = eMBSetSlaveID(0x34, TRUE, ucSlaveID, 0); - work fine
  2. This is what the compiler (gcc) thinks about it and I agree with him:
    warning: array subscript is above array bounds

P.S. you can try to write data with length equal to MB_FUNC_OTHER_REP_SLAVEID_BUF:
ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF] = 0xEE

@pvyleta
Copy link
Collaborator Author

pvyleta commented Jan 4, 2020

Wow, seems I was really short sighted and concerned about making my zero-length example compile. I will take a deeper look while at PC.

@pvyleta
Copy link
Collaborator Author

pvyleta commented Jan 9, 2020

@KVAnton-WEB I checked in detail over and over again, and cannot see the issue. Where exactly in the code you think the array subscript is outside the array boundraries? I suspect that is on the line 67, but I would argue with the compiler, that this part is only invoked if the subscript is not outside of the boundaries.

@JoelStienlet
Copy link

I have to agree with pvyleta.
in mbconfig.h: MB_FUNC_OTHER_REP_SLAVEID_BUF is the "Number of bytes which should be allocated for the Report Slave ID command."
So it should be OK to store up to "MB_FUNC_OTHER_REP_SLAVEID_BUF" bytes in this buffer.
Now the test in question is:
if( usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF )
here you compare the size that you will use, "usAdditionalLen + 2", with the actual size of the buffer. (this is NOT an index, but the number of bytes to be stored!) There is no reason why the buffer couldn't be completely filled, so "<=" seems good to me too.

@KVAnton-WEB
Copy link

I take my hat off, it seems you're really right, and I'm confused as the author of the original code.

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

Successfully merging this pull request may close these issues.

3 participants