> The change to have all STRING_CSTs NUL terminated (but that NUL
> termination not necessarily inclided in STRING_LENGTH) is a good
> one.
> 
> I'm not sure how we can reliably verify NUL termination after the
> fact though and build_string already makes sure to NUL terminate
> STRING_CSTs.  So if GO strings are not NUL terminated then
> the STRING_CSTs still are.

The advantage is that there are less variations how string literals look
like in the middle end.  We will have a simple way to determine if
a string literal is NUL terminated or not.  And checking that property
in varasm.c is exactly the right thing to do.

String literals always have an array_type which may be shorter
than TREE_STRING_LENGTH, but that chops off only exactly
one wide character nul. Otherwise if the array_type is equal or larger,
we know for sure the value is nul terminated. In the middle-end
we can easily determine if a string is not NUL terminated by:

compare_tree_int (TYPE_SIZE_UNIT (TREE_TYPE (init)),
                       TREE_STRING_LENGTH (init)) < 0

I did use that already in my patch for pr86711.

Additionally not having oversize string constants produced
by the front ends, where the extra characters are good for nothing,
also helps to improve correctness.


Bernd.

Reply via email to