The z_owned_stirng_t type contains string which is not null terminated. But It's very typical error to use z_string_data() with e.g. %s type specifier of for strcmp(). With AI-based code autocompletion it happens even more easily. We need to take some measures to prevent this kind of bugs:
Necessary actions:
- fix documentation:
- say in doc for type
z_owned_string_t itself that sting is NON-null terminated
- say that
z_owned_string_t is NON-null terminated in z_string_data()
- rephrase
z_string_len() help: phrase "the length of the string (without terminating 0 character)." is misleading
- review docuentation
Proposed API update:
- upgrade
z_owned_string_t type to allow it to store null-terminated strings:
- add boolean flag "is null-terminated" (we can use one bit of size field for this to not extend the structure size)
- add function
const char* z_string_as_cstr(z_loaned_string_t* s). This function will either just return pointer to string data (if string is already null-terminated) or reallocate passed string s (notice that pointer in non-const) by adding zero to the end and setting the null-terminated flag.
- add function
z_string_is_cstr(const z_loaned_string_t*) which returns this flag
This update will provide a direct way to get null-terminated string which is the frequent need of C developers
The
z_owned_stirng_ttype contains string which is not null terminated. But It's very typical error to usez_string_data()with e.g.%stype specifier of forstrcmp(). With AI-based code autocompletion it happens even more easily. We need to take some measures to prevent this kind of bugs:Necessary actions:
z_owned_string_titself that sting is NON-null terminatedz_owned_string_tis NON-null terminated inz_string_data()z_string_len()help: phrase "the length of the string (without terminating 0 character)." is misleadingProposed API update:
z_owned_string_ttype to allow it to store null-terminated strings:const char* z_string_as_cstr(z_loaned_string_t* s). This function will either just return pointer to string data (if string is already null-terminated) or reallocate passed strings(notice that pointer in non-const) by adding zero to the end and setting the null-terminated flag.z_string_is_cstr(const z_loaned_string_t*)which returns this flagThis update will provide a direct way to get null-terminated string which is the frequent need of C developers