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

MFRC522Extended.cpp:824: warning: ordered comparison of pointer with integer zero #371

Open
szotsaki opened this issue Apr 10, 2018 · 15 comments
Labels
bug 🐛 a not intended feature help_wanted 💉 here you can help others or/and make pull requests

Comments

@szotsaki
Copy link

szotsaki commented Apr 10, 2018

The following GCC warning is emitted during compile:

MFRC522Extended.cpp:824: warning: ordered comparison of pointer with integer zero [-Wextra]
  if (backData && (backLen > 0)) {
                             ^

Since backLen is a pointer, it will be always positive. However, for fixing it I don't know if it's a bug

  • not being backLen != nullptr or
  • not being *backLen > 0 or
  • just a slight oversight and the comparison is unnecessary.

The same is also in line 847.

@Rotzbua Rotzbua added bug 🐛 a not intended feature help_wanted 💉 here you can help others or/and make pull requests labels Apr 10, 2018
@Rotzbua
Copy link
Collaborator

Rotzbua commented Apr 10, 2018

I agree that there is a unclear situation. But I have no overview about this code. Main coding is from JPG-Consulting but he finished his project.

Links to lines of code:
https://github.com/miguelbalboa/rfid/blame/master/src/MFRC522Extended.cpp#L824
https://github.com/miguelbalboa/rfid/blame/master/src/MFRC522Extended.cpp#L847

@JM-FRANCE
Copy link

JM-FRANCE commented Feb 22, 2020

My guess is that it is to check that the pointer is non NULL as you read from/write to the pointed address in case the if condition is true.

--> just change if (backData && (backLen > 0)) into if (backData && (backLen != nullptr))

(edit - disregard)

@RalphCorderoy
Copy link

Please consider reopening this issue as it blocks compiling an Arduino sketch with -Werror and ignoring warnings is generally a bad smell because other warnings mingle in with the known ones and get missed.

Besides, I think looking at the code shows up a bunch of problems in this area which suggest it has no users and could just be deleted or #ifdef'd out until it's all fixed.

The prototype allows two optional parameters, both pointers, both defaulting to NULL.

StatusCode TCL_Transceive(TagInfo * tag, byte *sendData, byte sendLen,
    byte *backData = NULL, byte *backLen = NULL);

The function body always unconditionally dereferences backLen.

byte totalBackLen = *backLen;

I don't know C++, only C, but I assume this is just as invalid in C++?

That implies backLen is never allowed to be NULL and thus must always be given but I don't think that's the author's intent so it's probably a bug and the parameter is intended to be optional and so NULL.

Then we have the two uses which guard the memcpy(3), e.g.

if (backData && (backLen > 0)) {
        if (*backLen < in.inf.size)
                return STATUS_NO_ROOM;

        *backLen = in.inf.size;
        memcpy(backData, in.inf.data, in.inf.size);
}

Given backLen may be NULL the if-condition must guard against it. The test that the length is big enough is already in the body and correct so I think this just needs to change to

if (backData && backLen) {

Similarly for the second case:

if (backData && (backLen > 0)) {
        if ((*backLen + ackDataSize) > totalBackLen)
                return STATUS_NO_ROOM;

        memcpy(&(backData[*backLen]), ackData, ackDataSize);
        *backLen += ackDataSize;
}

We've already saved the original size of backData by copying *backLen into totalBackLen and have altered *backLen to reflect what we've used so far so the body's test takes all that into account and adjusts memcpy()'s destination. All that's left is to correct the guard as before. Changing the test to *backLen > 0 wouldn't be much use and wouldn't allow for in.inf.size to be 0 in the first memcpy.

@salieff, does this seem accurate to you given you've submitted a patch in the past?

@JM-FRANCE
Copy link

backLen is a pointer, writing (backLen > 0) is not correct
It drives the warning "ordered comparison of pointer with integer zero"

@RalphCorderoy
Copy link

Hi @JAY-M-ERICSSON, Yes, that's the problem being fixed. You seem to be taking a quote of the current faulty code as a suggestion for the fix. :-)

@JM-FRANCE
Copy link

fair point indeed

@pktl
Copy link

pktl commented Dec 7, 2021

Hi everyone,
I encountered this issue with a twist: What you described as a warning message is an error on my machine, preventing me from compiling at all.

/home/pb/Arduino/libraries/MFRC522/src/MFRC522Extended.cpp: In member function 'MFRC522::StatusCode MFRC522Extended::TCL_Transceive(MFRC522Extended::TagInfo*, byte*, byte, byte*, byte*)':
/home/pb/Arduino/libraries/MFRC522/src/MFRC522Extended.cpp:824:34: error: ordered comparison of pointer with integer zero ('byte*' {aka 'unsigned char*'} and 'int')
  824 |         if (backData && (backLen > 0)) {
      |                          ~~~~~~~~^~~
/home/pb/Arduino/libraries/MFRC522/src/MFRC522Extended.cpp:847:42: error: ordered comparison of pointer with integer zero ('byte*' {aka 'unsigned char*'} and 'int')
  847 |                 if (backData && (backLen > 0)) {
      |                                  ~~~~~~~~^~~

I'm using Arduino IDE 1.8.16 and MFRC522 v1.4.10, if that matters.
Are there any compiler flags that might be set differently on my machine?

This is what's executed on compile:

/usr/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10816 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/archlinux-arduino/avr/cores/arduino -I/usr/share/arduino/hardware/archlinux-arduino/avr/variants/eightanaloginputs -I/home/pb/Arduino/libraries/DFPlayer_Mini_Mp3_by_Makuna/src -I/usr/share/arduino/hardware/archlinux-arduino/avr/libraries/EEPROM/src -I/home/pb/Arduino/libraries/JC_Button/src -I/home/pb/Arduino/libraries/MFRC522/src -I/usr/share/arduino/hardware/archlinux-arduino/avr/libraries/SPI/src -I/usr/share/arduino/hardware/archlinux-arduino/avr/libraries/SoftwareSerial/src /home/pb/Arduino/libraries/MFRC522/src/MFRC522Extended.cpp -o /tmp/arduino_build_940207/libraries/MFRC522/MFRC522Extended.cpp.o

@JM-FRANCE
Copy link

Hi everyone, I encountered this issue with a twist: What you described as a warning message is an error on my machine, preventing me from compiling at all.

have you modified the library taking into account the suggestion from @RalphCorderoy?

#371 (comment)

@pktl
Copy link

pktl commented Dec 8, 2021

Ah, my mistake. Didn't read it as careful as I should have. I got it to compile with that fix.
Thanks!

FallenAngel666 added a commit to FallenAngel666/rfid that referenced this issue Mar 1, 2022
@FallenAngel666
Copy link

Please consider reopening this issue as it blocks compiling an Arduino sketch with -Werror and ignoring warnings is generally a bad smell because other warnings mingle in with the known ones and get missed.

Besides, I think looking at the code shows up a bunch of problems in this area which suggest it has no users and could just be deleted or #ifdef'd out until it's all fixed.

The prototype allows two optional parameters, both pointers, both defaulting to NULL.

StatusCode TCL_Transceive(TagInfo * tag, byte *sendData, byte sendLen,
    byte *backData = NULL, byte *backLen = NULL);

The function body always unconditionally dereferences backLen.

byte totalBackLen = *backLen;

I don't know C++, only C, but I assume this is just as invalid in C++?

That implies backLen is never allowed to be NULL and thus must always be given but I don't think that's the author's intent so it's probably a bug and the parameter is intended to be optional and so NULL.

Then we have the two uses which guard the memcpy(3), e.g.

if (backData && (backLen > 0)) {
        if (*backLen < in.inf.size)
                return STATUS_NO_ROOM;

        *backLen = in.inf.size;
        memcpy(backData, in.inf.data, in.inf.size);
}

Given backLen may be NULL the if-condition must guard against it. The test that the length is big enough is already in the body and correct so I think this just needs to change to

if (backData && backLen) {

Similarly for the second case:

if (backData && (backLen > 0)) {
        if ((*backLen + ackDataSize) > totalBackLen)
                return STATUS_NO_ROOM;

        memcpy(&(backData[*backLen]), ackData, ackDataSize);
        *backLen += ackDataSize;
}

We've already saved the original size of backData by copying *backLen into totalBackLen and have altered *backLen to reflect what we've used so far so the body's test takes all that into account and adjusts memcpy()'s destination. All that's left is to correct the guard as before. Changing the test to *backLen > 0 wouldn't be much use and wouldn't allow for in.inf.size to be 0 in the first memcpy.

@salieff, does this seem accurate to you given you've submitted a patch in the past?

Confirm that. Works fine after change. Also Line 909 the creator of the code already uses the proper comparison.

FallenAngel666 added a commit to FallenAngel666/rfid that referenced this issue Mar 1, 2022
Instead we just look if backLen is set. 
More infos here: miguelbalboa#371
@ZevEisenberg
Copy link

Bump - any chance of getting this fixed?

@SteveJ789
Copy link

I have had this issue when trying to compile the code for a Raspberry Pi Pico W using Earle Philhowers Arduino-Pico core. The fix for me was to change

if (backData && (backLen > 0)) {

to

if (backData && backlen != nullptr)) {

at lines 824 and 847. The code now compiles and the sketch runs correctly.

mhaberler pushed a commit to mhaberler/Arduino_MFRC522v2 that referenced this issue Dec 14, 2023
leopold-gravier added a commit to leopold-gravier/rfid-from-miguelbalboa that referenced this issue Mar 25, 2024
@arnaldomacari
Copy link

For esp32
in the file: /Arduino/libraries/MFRC522/src/MFRC522Extended.cpp

Line: 824 and 847
replace: if (backData && (backLen > 0))
by : if (backData && backLen != nullptr)

@RalphCorderoy
Copy link

That looks to be equivalent to the fix I suggest in the analysis above: #371 (comment)

marcosfnsc added a commit to lada-uern/rfid that referenced this issue Sep 2, 2024
@DanZkai794
Copy link

Eu tive esse problema ao tentar compilar o código para um Raspberry Pi Pico W usando o núcleo Arduino-Pico de Earle Philhowers. A solução para mim foi mudar

se (backData && (backLen > 0)) {

para

se (backData && backlen != nullptr)) {

nas linhas 824 e 847. O código agora compila e o esboço é executado corretamente.

the Hello, I have the same problem with my Arduino IDE. How can I change these lines?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 a not intended feature help_wanted 💉 here you can help others or/and make pull requests
Projects
None yet
Development

No branches or pull requests

10 participants