glaze icon indicating copy to clipboard operation
glaze copied to clipboard

Reading and Writing binary to std::vector fails with `data_must_be_null_terminated`

Open misyltoad opened this issue 1 year ago • 2 comments

Doing some simple code like:

    std::shared_ptr<Config> ConfigManager::LoadAll()
    {
        std::optional<glz::json_t> oJsonConfig = LoadAllJson();
        if ( !oJsonConfig )
            return nullptr;

        std::vector<uint8_t> mergedConfig;
        {
            auto ec = glz::write_binary( *oJsonConfig, mergedConfig );
            if ( ec )
            {
                std::string_view psvEnumName = glz::nameof( ec.ec );
                s_ConfigLog.errorf( "Failed to merge configs: %.*s",
                    int ( psvEnumName.size() ), psvEnumName.data() );
                return nullptr;
            }
        }

        std::shared_ptr<Config> pNewConfig = std::make_shared<Config>();
        {
            auto ec = glz::read_binary( *pNewConfig, mergedConfig );
            if ( ec )
            {
                std::string_view psvEnumName = glz::nameof( ec.ec );
                s_ConfigLog.errorf( "Failed to deserialize configs: %.*s",
                    int ( psvEnumName.size() ), psvEnumName.data() );
                return nullptr;
            }
        }

        return pNewConfig;
    }

Seems to fail, with read_binary returning data_must_be_null_terminated.

Using auto ec = glz::read_binary( *pNewConfig, std::span<uint8_t>{ mergedConfig } ); seems to get further, but fails with syntax_error for me.

I tried adding error_on_unknown_keys=true using read/write with binary, but it didn't help.

misyltoad avatar Jul 28 '24 14:07 misyltoad

Thanks for this error report with examples and code. Will have to look into this.

stephenberry avatar Jul 28 '24 22:07 stephenberry

If you can use a std::string instead of std::vector<uint8_t> for mergedConfig then you can ensure null termination. Or, you could just push_back a null character to your std::vector<uint8_t>.

If you could make a simple example replicating your issue on compiler explorer that would help me point out the exact problem. But, you are probably just missing a null terminating character on your mergedConfig.

stephenberry avatar Aug 08 '24 02:08 stephenberry