Skip to content

provide null-terminated z_owned_sting_t variant #1037

@milyin

Description

@milyin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions