espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.59k stars 7.27k forks source link

result of 16bit unsigned arithmetics treated as signed (IDFGH-13747) #14609

Closed caspehre closed 1 month ago

caspehre commented 1 month ago

Answers checklist.

General issue report

It seems result of unsigned short arithmetics is treated as signed. Consider following code, the u32 example works, but not the u16 example.

volatile unsigned long u32integer1 = 10; volatile unsigned long u32integer2 = 0xFFFFFFFF;

volatile unsigned short u16integer1 = 10; volatile unsigned short u16integer2 = 0xFFFF;

void AppTask(void){ if ((u32integer1 - u32integer2) > 10){ printf("u32 works\n"); }else{ printf("u32 does not work\n"); } if ((u16integer1 - u16integer2) > 10){ printf("u16 works\n"); }else{ printf("u16 does not work\n"); } }

igrr commented 1 month ago

That's the correct observation, but it's not specific to ESP-IDF.

caspehre commented 1 month ago

Ok, then I learned something new, I thought it was reasonable to assume second example should work. I guess this issue is closed then?

caspehre commented 1 month ago

Since this seems to be something not specific to xtensa, I close this issue

igrr commented 1 month ago

Yeah, that's just one the many of C language's footguns, unfortunately.