Bug 403404 - An integer overflow when interpreting ULONG_MAX
Summary: An integer overflow when interpreting ULONG_MAX
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-editor (show other bugs)
Version: Next   Edit
Hardware: PC Linux
: P3 critical with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
: 426741 526414 559622 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-03-14 19:36 EDT by David W CLA
Modified: 2020-01-28 17:14 EST (History)
13 users (show)

See Also:


Attachments
Please see that checkpoint1 and checkpoint2 are greyed out which shouldn't (108.67 KB, image/jpeg)
2013-03-14 19:36 EDT, David W CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description David W CLA 2013-03-14 19:36:27 EDT
Created attachment 228453 [details]
Please see that checkpoint1 and checkpoint2 are greyed out which shouldn't

Hi 

I've found a problem and I think it might be an eclipse bug. Please see my attachment.

I am on redhat 5 x64 running juno. The following code prints out checkpoint2. However, eclipse grayed out checkpoint1 and checkpoint2 . Appraently, eclipse uses a different ULONG_MAX value as from the gcc compiler. Is this a bug from eclipse?

#include <stdio.h>
#include <limits.h>

int main()
{
  printf("%llu\n", (unsigned long long) ULONG_MAX);
  /* prints 18446744073709551615 and checkpoint2 */
  /* but hover mouse over, the ULONG_MAX shows (9223372036854775807L * 2UL + 1UL) */

  #if (ULONG_MAX >> 31) == 1     <---- grayed out
    printf("checkpoint1\n");
  #elif (ULONG_MAX >> 63) == 1   <---- grayed out
    printf("checkpoint2\n"); 
  #else
    printf("checkpoint3\n");
  #endif

  return 0;
}
Comment 1 David W CLA 2013-03-21 13:06:12 EDT
Is this a bug? Can someone please try it and see if you see the same thing? Thanks
Comment 2 Klaus Klaus CLA 2013-03-22 13:51:28 EDT
Hi,

i guess that the issue is, that the biggest integer Data Type in Java is long, which is a 64 Bit SIGNED type.

This example shows that:

#include <stdio.h>
#include <limits.h>

#define TEST_MAX	0x7fffffffffffffffUL

int main() {
	printf("%llu\n", (unsigned long long) TEST_MAX);

#if (TEST_MAX >> 31) == 1
	printf("checkpoint1\n");
#elif (TEST_MAX >> 62) == 1
	printf("checkpoint2\n");
#else
	printf("checkpoint3\n");
#endif

	return 0;
}

CDT should support longer Integer and Unsigned Integers up to 128 Bit.
Comment 3 David W CLA 2013-03-22 14:37:29 EDT
Hi Klaus

Thanks. I don't see an existing bug so I created one last week, ID 403404.

I used your example and yes the checkpoint2 is NOT grayed out.

My case is not as simple as printing out "checkpoinsts". I actually have some typedef within each if condition. Wrongly grayed out means the typedef declaration within the if statement will NOT be known and so the indexing throughout the whole program fails.

Is there a quick fix that we can do to avoid this situation?

Thanks

David
Comment 4 Marc-André Laperle CLA 2014-02-05 17:40:31 EST
*** Bug 426741 has been marked as a duplicate of this bug. ***
Comment 5 Nathan Ridge CLA 2017-10-24 19:35:06 EDT
*** Bug 526414 has been marked as a duplicate of this bug. ***
Comment 6 Nathan Ridge CLA 2017-10-24 19:38:57 EDT
Now that we use Java 8, we may be able to fix this without having to resort to something like BigInteger for our value representation, since the Long class now provides utilities for treating Long values as 64-bit unsigned integers (see e.g. https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#compareUnsigned-long-long-).
Comment 7 Nathan Ridge CLA 2017-12-30 17:39:59 EST
(In reply to Nathan Ridge from comment #6)
> Now that we use Java 8, we may be able to fix this without having to resort
> to something like BigInteger for our value representation, since the Long
> class now provides utilities for treating Long values as 64-bit unsigned
> integers (see e.g.
> https://docs.oracle.com/javase/8/docs/api/java/lang/Long.
> html#compareUnsigned-long-long-).

There are two challenges to implementing a solution along these lines:

  1. The way CDT currently implements computations
     with values, by the time we get into the code
     that performs the actual computations, the
     information about the *types* of the values is
     lost. (In fact, for some codepaths, the types
     of the values are not resolved to begin with.)

     Having the types available is important because
     e.g. using compareUnsigned() would give wrong
     results if the values were, in fact, signed.

     So the code would need to be reworked to ensure 
     that the types are always resolved, and propagated 
     to the code that performs the computation.

  2. While the Long class provides *some* unsigned
     operations, such as compareUnsigned, divideUnsigned,
     and remainderUnsigned, it doesn't provide all
     operations.

     For example, for the testcase in comment 0, we
     would need a "rightShiftUnsigned" operation.
Comment 8 Sergey Prigogin CLA 2018-01-01 21:17:20 EST
(In reply to Nathan Ridge from comment #7)
Right shift unsigned is just >>>, isn't it?
Comment 9 Nathan Ridge CLA 2018-01-01 21:42:09 EST
(In reply to Sergey Prigogin from comment #8)
> Right shift unsigned is just >>>, isn't it?

Looks like it. I just didn't know Java had such an operator :)

So I think part #2 from the previous comment can be disregarded. The problematic code examples we've encountered so far just seem to need "compareUnsigned" and ">>>".
Comment 10 Nathan Ridge CLA 2020-01-28 11:05:56 EST
*** Bug 559622 has been marked as a duplicate of this bug. ***
Comment 11 Christian Banse CLA 2020-01-28 14:59:55 EST
Thanks for Nathan to pointing me to the original bug report, shame on me for not finding it in the first place.

I came across the issue while parsing large literals. I will try to dig a little deeper into the issue and see if I can at least propose a solutions.

Unfortunately I am completely new to the Eclipse world from the viewpoint of a contributor (I am quite used to the pull request-approach-style of GitHub projects) so any tips into directions of contributing guides and so on are welcome.
Comment 12 Nathan Ridge CLA 2020-01-28 17:14:23 EST
(In reply to Christian Banse from comment #11)
> I came across the issue while parsing large literals. I will try to dig a
> little deeper into the issue and see if I can at least propose a solutions.
> 
> Unfortunately I am completely new to the Eclipse world from the viewpoint of
> a contributor (I am quite used to the pull request-approach-style of GitHub
> projects) so any tips into directions of contributing guides and so on are
> welcome.

You can find some docs on contributing here:

https://wiki.eclipse.org/CDT/contributing

The place to start looking in the code would be ValueFactory methods like applyBinaryOperator() and applyUnaryOperator().