1

i decompiled some code and stumbled across an if-statement which I cannot make sense of.

if (y < '\0') 

with y = 0xED.

As far as I understand, '\0' is NULL or 0x00. In my mind, it would not make sense to compare a number to a character but even if so, it would never enter the routine since it cannot be less than 0x00.

My question: Does this statement make sense? If so, in what cases would the routine be entered? Or if this statement cannot be entered, what is the reasoning behind writing the whole routine at all? Are there instances where it does make sense to compare a number to a character?

Edit: y is fixed to 0xED but it is in a memory location which can be written to via NFC. From the answers I got that the type of y is probably a signed char/int.

HBHugo
  • 11
  • 2
  • 2
    It depends on the context. Is `y` fixed to `0xED`? What is the type of `y`? – MikeCAT Feb 25 '21 at 15:25
  • 5
    If `y` is a `signed char` it would contain the value `-19`. Also character constants have type `int` which can be compared with other `ints` without a problem. – Gerhardh Feb 25 '21 at 15:29
  • It makes sense. Whether it does anything useful depends on whether or not `y` is either a signed `char` or a plain `char` on a machine where that is a signed type. If it is not a signed `char`, the test will always fail. If it is a signed `char` (and CHAR_BIT` is 8), then the test will evaluate to true. – Jonathan Leffler Feb 25 '21 at 15:30
  • It is also entirely possible that `y` is just being used as some signed integer and this test is "really" `if (y < 0)`. There is no difference in machine code between the zero byte and the null character, so the decompiler is just guessing which one the programmer might have written. – Nate Eldredge Feb 25 '21 at 15:35
  • If you don't think `0xED` is less than zero, can you give an example of a number *expressed in that format* that is negative? Do you think it's impossible to express a negative number in `0x...` format? – David Schwartz Feb 25 '21 at 15:40
  • Show us the appropriate portion of the disassembly listing. That will enable people to provide an accurate answer. – fpmurphy Mar 03 '21 at 02:49

0 Answers0