Opened 6 years ago
Last modified 5 years ago
#189 closed defect
Reference types allow unsound assignment — at Initial Version
| Reported by: | mlbrooks | Owned by: | |
|---|---|---|---|
| Priority: | major | Component: | cfa-cc |
| Version: | 1.0 | Keywords: | |
| Cc: |
Description
This code implies a reinterpret pointer cast. But the code does not do a cast.
int main() {
float x = 3.14;
float & xx = x;
int & yy = xx; // not sound
int y = yy;
printf("%d\n", y);
}
Expected: Compiler error at "not sound" line, saying cannot initialize int & from float &.
Actual: Compiler success; program prints 1078523331 which is the decimal value of 0x4048f5c3, which is the IEEE 754 representation of 3.14.
Note that this unsoundness also happens at function return.
This allows for:
forall (dtype T, dtype S)
T & anycvt( S & s ) {
return s; // not sound
}
int main() {
float x = 3.14;
int y = anycvt(x);
printf("%d\n", y);
}
Expected: Compiler error at "not sound" line, saying cannot initialize T & from S &.
Actual: Compiler success; program prints 1078523331.
Note:
See TracTickets
for help on using tickets.