Skip to content

String length and allocation #6

Open
@Sod-Almighty

Description

@Sod-Almighty

When calling the readString functions, I have to magically know how big the string buffer needs to be. There appears to be no way to check this ahead of time.

I propose a variant of readString, thusly:

char* readNewString(Stream& stream, bool safely = true) {
 DataType dataFormat;
 getNextDataType(stream, dataFormat, safely);
 size_t outputSize;
 char* value = nullptr;
 switch(dataFormat) {
  case DataType::String5: {
   MSGPACK_SAFETY_FORMAT_CHECK(DataType::String8);
   stream.read();
   MSGPACK_SAFELY_RUN(readRaw(stream, outputSize, safely));
   value = new char[outputSize + 1];
   MSGPACK_SAFELY_RUN(readRaw(stream, value, outputSize, safely));
   value[outputSize] = '\0';
   return value;
  }
  /* ... */
 }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions