On Wednesday, 1 July 2020 at 05:04:28 UTC, Anthony wrote:
I'm trying to convert this c function:
bson_t *bson_new_from_json (const uint8_t *data, ssize_t len,
bson_error_t *error);
Into a D function. This is my attempt:
extern(C) {
struct bson_t;
struct bson_error_t;
bson_t* bson_new_from_json(const uint8_t* data, long len,
bson_error_t* error);
}
However when I try it, for example:
auto str_utf8 = str.toUTF8();
bson_error_t error
auto bson = bson_new_from_json(cast(const
uint8_t*)str_utf8.ptr, -1, &error);
I get a "Program exited with code -11" message.
Does anyone know what I'm doing wrong?
Thanks
I don't know the exact function you are trying to use, but -11
means "segmentation fault" on linux. This means that your program
is trying to read or write a memory location that it is not
supposed to. This typically happens during buffer overflows and
similar memory corruption bugs.
One thing that jumps to me is the -1 in your call instead of the
length. Without knowing the C function's implementation I would
expect it to mean either "read before the array" which would be a
buffer overflow or to have the special meaning of "deduce the
string size yourself". In that last case I would expect
bson_new_from_json to expect a NUL-terminated array, but I don't
know if your UTF8 array is NUL-terminated.