3

In my C++ code I have an expression where I multiply unsigned long integer to an int to assign the result to unsigned long int. I am getting a warning as 'overflowed returned value'. I tried to cast the int to unsigned long but it did not help. Any suggestions...

The expression is something like this-

uint64_t size = 0;
uint64_t value = getvalue();
int pageSize= getPageSize();
size = value*(uint64_t)pageSize;
8
  • 6
    Can you paste your code here? Commented Aug 31, 2011 at 8:56
  • If overflow is intended and you just want to suppress the warning, try to use unsigned int instead of plain int. Commented Aug 31, 2011 at 9:10
  • overflow is not intended and the function which return plain int can not be changed to return unsigned long neither the return value 'size'. Commented Aug 31, 2011 at 9:12
  • Which compiler and version are we talking about here? This doesn't repro with gcc/g++ 4.5.2. And I tried with -Wall, -Wextra, and -pedantic and variations of -Wstrict-overflow=N Commented Aug 31, 2011 at 9:21
  • This appears in a static code checker tool.. Commented Aug 31, 2011 at 9:23

2 Answers 2

1

The following should do the job:

size = value*(uint64_t)(unsigned int)pageSize;
Sign up to request clarification or add additional context in comments.

Comments

0

Your tool probably has a way to set an exception for this line of code.

If the tool is truly clever, this is the only way, for there really is a risk of overflow here (as an int may not accomodate 2^64-1).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.