Skip to content

Empty strings cannot be written in msgpack-cxx #1164

@MusicScience37

Description

@MusicScience37

Describe the bug

msgpack-cxx 7.0.0 throws an exception when writing an empty string using fbuffer.
The exception is thrown at the following if statement.

if (1 != fwrite(buf, len, 1, m_file)) {
throw std::runtime_error("fwrite() failed");
}

To Reproduce

I ran the following using msgpack-cxx 7.0.0 and Clang 19.1.7.

#include <cstdio>

#include <msgpack.hpp>
#include <msgpack/fbuffer.hpp>

int main() {
    std::FILE* file = std::fopen("test_error_in_msgpack.data", "wb");
    if (file == nullptr) {
        std::perror("Failed to open file");
        return 1;
    }
    msgpack::fbuffer buffer(file);
    msgpack::packer<msgpack::fbuffer> packer(buffer);
    packer.pack(std::string());
    return 0;
}

Expected behavior

No exception should happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions