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

warning: comparison of integer expressions of different signedness #895

Open
SteveRMann opened this issue Sep 30, 2021 · 2 comments
Open

Comments

@SteveRMann
Copy link

SteveRMann commented Sep 30, 2021

I know it's "just a warning", but it is so easy to fix.

Complete warning:

C:\Users\steve\Documents\Arduino\libraries\PubSubClient\src\PubSubClient.cpp:523:16: warning: comparison of integer expressions of different signedness: 'unsigned int' and 'int' [-Wsign-compare]
  523 |     return (rc == expectedLength);
      |             ~~~^~~~~~~~~~~~~~~~~

Solution:
In PubSubClient.cpp, at line 481, change unsigned int rc to uint16_t rc

@m1cje
Copy link

m1cje commented Jun 6, 2022

unsigned int rc is a 32 bit value, changing it to uint16_t may have unforeseen consequences, better to change expectedLength to be unsigned as it's unlikely to ever be negative

@stefan123t
Copy link

stefan123t commented Aug 19, 2022

The suggested simple fix by @m1cje to declare unsigned int expectedLength in

int expectedLength;

is obviously valid as:

expectedLength = 1 + llen + 2 + tlen + plength;
return (rc == expectedLength);

All of llen, tlen and plength are uint8/16_t or unsigned int either. Hence expectedLength cannot become negative at all.

Thanks for providing and maintaining this library in the first place!

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

3 participants