Skip to content

Compiler complains about SX1509::readWord(uint8_t registerAddress, uint16_t *value) #19

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

Open
PKeller34 opened this issue May 11, 2023 · 0 comments

Comments

@PKeller34
Copy link

I'm using Arduino 2.1.0 and version 3.0.4 of the SparkFunSX1509 library.

When compiling using #include <SparkFunSX1509.h> the compiler complains about an out of bounds array reference:

.....SX1509_IO_Expander\src\SparkFunSX1509.cpp:790:26: warning: array subscript 1 is outside array bounds of 'uint16_t [1]' {aka 'short unsigned int [1]'} [-Warray-bounds]
790 | value[1] = dest[0];
| ~~~~~~~~~^~~~~~~~~

There are a cascade of other complaints that are all triggered by the same basic issue: referencing a u_int16 as an array of u_int8[2]. The compiler understands what is intended, but complains because now it's stricter about types.

Replacing the code:

	value[0] = dest[1];
	value[1] = dest[0];

with:

           value[0] = (dest[1] << 8) + dest[0];

does the deed in a way that directly reflects the intent and keeps the compiler happy.

So the updated method looks like this (old code commented out):

bool SX1509::readWord(uint8_t registerAddress, uint16_t *value)
{
uint8_t dest[2];
if (readBytes(registerAddress, dest, 2))
{
// value[0] = dest[1];
// value[1] = dest[0]; // "value[1]" is actually not defined, except in classic C
value[0] = (dest[1] << 8) + dest[0]; // Type correct version
return true;
}
return false;
}

Since I'm not yet experienced in git-ism's I am reluctant to just submit a pull request and make this change myself so I guess I'm punting this to the experts at SparkFun:-)

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

1 participant