Opened 6 years ago

Closed 6 years ago

#59 closed defect (fixed)

Unsized generics have incorrect pointer arithmetic

Reported by: Thierry Delisle Owned by: Rob Schluntz <rschlunt@…>
Priority: major Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description

The following program produces an incorrect result:

#include <fstream>

forall(dtype T)
struct wrapper{
	T * ptr;
	int size;
};

typedef long long int type_t;

int main() {
	type_t a[10];
	wrapper(type_t) w = {a, 10};

	type_t * start_w = w.ptr;
	type_t * end_w   = w.ptr + w.size;

	sout | start_w | "+ (" | w.size | "*" | sizeof(type_t) | ") == " | end_w | endl;
}

The result should be :

0x7fffb3dc1d50 + (10 * 8) == 0x7fffb3dc1da0

The actual result is :

0x7fffb3dc1d50 + (10 * 8) == 0x7fffb3dc1d5a

(Addresses may vary)

This is because the size of the type is not used.
The problem cause away by adding the "sized" constraint to the generic type.

Possible fixes:

  • Disallow pointer arithmetic in this case.
  • Somehow get the size.

Change History (2)

comment:1 Changed 6 years ago by Rob Schluntz

I think this is related to #42. In both cases, adding a cast fixes the problem.

type_t * end_w   = ((typeof(w.ptr))w.ptr) + w.size;

result:

0x7ffe45823440 + (10 * 8) == 0x7ffe45823490

Perhaps when dtype-static generics are monomorphized, the extra cast should be added to any uses of the dtype member?

comment:2 Changed 6 years ago by Rob Schluntz <rschlunt@…>

Owner: set to Rob Schluntz <rschlunt@…>
Resolution: fixed
Status: newclosed

In b95fe40:

Add casts to dtype-static member expressions to prevent loss of type information [fixes #42] [fixes #59]

Note: See TracTickets for help on using tickets.