Netocrat said:
[can a string with length > SIZE_MAX be portably created; and if so what
is the result of calling strlen on it]
[whose product is intended to be greater than SIZE_MAX]
It did not.
That's what I thought, and why I did not use it.
N1124, 6.5.6#8 (on pointer arithmetic)
| If both the pointer operand and the result point to elements of the
| same array object, or one past the last element of the array object,
| the evaluation shall not produce an overflow; otherwise, the
| behavior is undefined. If the result points one past the last
| element of the array object, it shall not be used as the operand of a
| unary * operator that is evaluated.
The "array object" is an array of char (the type to which ptr points),
whereas calloc returns an array of arrays of char. As soon as the ptr
reaches one beyond the end of the first array and is dereferenced, it
is breaking the "shall" condition; and once it reaches two beyond the
end of the first array, it is explicitly undefined behaviour.
I disagree because an array is an object so this applies from N1124,
6.3.2.3 Pointers:
| 7 A pointer to an object or incomplete type may be converted to a
| pointer to a different object or incomplete type. If the resulting
| pointer is not correctly aligned57) for the pointed-to type, the
| behavior is undefined. Otherwise, when converted back again, the
| result shall compare equal to the original pointer. When a pointer
| to an object is converted to a pointer to a character type, the
| result points to the lowest addressed byte of the object. Successive
| increments of the result, up to the size of the object, yield
| pointers to the remaining bytes of the object.
See above, and the definition of an object in N1124, 3.14:
| object
| region of data storage in the execution environment, the contents of
| which can represent values
[calloc returns an object]
Agreed.
Since you agree calloc returns an object, 6.3.2.3 applies when you
convert it to a pointer to a character type.
Although the DR's conclusion also says:
| Translation limits do not apply to objects whose size is determined
| at runtime.
Ah well.
That seems like better reasoning given what I quoted above from the DR re
translation limits not applying to runtime objects.
Indeed, which is why I mentioned it
The standard doesn't seem to require it; likewise DR 266. Regarding the
result of applying sizeof to such an oversize (determined at runtime)
object, the DR seems to say nothing though.
Well then, if that is the case then I would guess the calloc call is
required to fail or return a block that could be used for an oversize
string, it's just that functions line strlen are not required to work
this the oversize string.