Catching the unsigned int

A

Aandi Inston

Here's the problem: I have a macro which is called all over a large
program, and which will produce incorrect results if the argument is
an unsigned [long] int.

(It didn't originally, but an external header file changed).

What I was trying to do was catch this, by (for test purposes)
changing the macro and forcing a compiler error or warning if used
with an unsigned long int, rather than long int. This is proving more
challenging than I expected.

I tried

#undef THEMACRO
#define THEMACRO(x) CallProc(&x)
long int CallProc ( long int *x) ;

(Which gives a few errors on things that aren't lvalues but I can live
with that).

But it seems that (in this environment) I get no error if THEMACRO is
used for an unsigned int.

The code can be compiled in Microsoft Visual C++ or Metrowerks
CodeWarrior; as C or C++ language. (Setting up to compile with another
compiler would probably take more manually checking all references,
which this lazy programmer is trying to avoid).

(Background: the macro used to do x<<16 but now does
x < -32767 ? BAD_RESULT : x << 16.
So far as I can tell the effect of feeding this an unsigned int is
that the -32767 is treated as unsigned before doing the comparison.)

Thanks in advance,
 
P

Peter Nilsson

Aandi said:
Here's the problem: I have a macro which is called all over a large
program, and which will produce incorrect results if the argument is
an unsigned [long] int.

(It didn't originally, but an external header file changed).

What I was trying to do was catch this, by (for test purposes)
changing the macro and forcing a compiler error or warning if used
with an unsigned long int, rather than long int. This is proving
more challenging than I expected.

I tried

#undef THEMACRO
#define THEMACRO(x) CallProc(&x)
long int CallProc ( long int *x) ;

If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels.
(Which gives a few errors on things that aren't lvalues but I can
live with that).

Given the apparent purpose of the macro, why are you (presumably)
passing rvalues to it?
But it seems that (in this environment) I get no error if THEMACRO
is used for an unsigned int.

The code can be compiled in Microsoft Visual C++ or Metrowerks
CodeWarrior; as C or C++ language. (Setting up to compile with
another compiler would probably take more manually checking all
references, which this lazy programmer is trying to avoid).

(Background: the macro used to do x<<16 but now does
x < -32767 ? BAD_RESULT : x << 16.

So there is no function CallProc? If that's so, then you should not
have posting your earlier version. How about you show us the _real_
code you're using, rather than something which is almost but not
quite totally unlike the real code.

Note that left shifting signed integers is fraught with undefined
behaviour.
So far as I can tell the effect of feeding this an unsigned int is
that the -32767 is treated as unsigned before doing the comparison.)

Yes, that's because the arithmetic promotions require that an operation
between an unsigned int and an int invoke a promotion of the int to
unsigned int.

Show us the macro you actually have, and how you intend to use it.
Show us the invocation of the macro which is causing problems.
In other words, give us code, not descriptions.
 
A

Aandi Inston

Thanks for the reply.

If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels.
So there is no function CallProc? If that's so, then you should not
have posting your earlier version. How about you show us the _real_
code you're using, rather than something which is almost but not
quite totally unlike the real code.

I haven't explained myself very well. I don't want a replacement macro
for changing the code. I want to temporarily change the macro to do a
one off hunt for problems.

That's why it was being called in a few cases with things that aren't
lvalues, and why this isn't a problem. That's why it isn't an issue
that CallProc doesn't exist.
Note that left shifting signed integers is fraught with undefined
behaviour.

I didn't write the original macro. So far it seems to work, but I'll
do some more analysis.
Show us the macro you actually have, and how you intend to use it.

That part isn't the problem. I want to switch to using a different
macro where the argument is unsigned, and the aim here is to find
where to do that.
In other words, give us code, not descriptions.

I'd really prefer to avoid complications that arise from that - I see
already that the paragraph of background only complicated things - and
focus on the problem hunt only.
 
A

Aandi Inston

Peter Nilsson said:
If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels.

Thanks, this confirmation that it WAS a violation led me to try more
options and compilers; I finally got it to complain with CodeWarrior.

Problem solved. (After all that, there were no unsigned values beyond
the one which I had identified as causing incorrect results).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top