digitalmars.D.learn - Program exited with code -11 when calling
- Anthony (18/18) Jun 30 2020 I'm trying to convert this c function:
- Cym13 (13/31) Jun 30 2020 I don't know the exact function you are trying to use, but -11
- Anthony (18/59) Jun 30 2020 Thanks for getting back to me.
- Kagamin (7/7) Jun 30 2020 bson_t* bson_new_from_json(in char* data, long len, bson_error_t*
- H. S. Teoh (8/17) Jun 30 2020 D strings are generally not null-terminated (except for literals).
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
Jun 30 2020
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? ThanksI 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.
Jun 30 2020
On Wednesday, 1 July 2020 at 05:09:47 UTC, Cym13 wrote:On Wednesday, 1 July 2020 at 05:04:28 UTC, Anthony wrote:Thanks for getting back to me. Yeah I figured it was a segmentation fault, however, I don't know exactly how to pinpoint where this is happening. I'm wondering if there's anything wrong with how I'm casting the data since everything is self contained (assuming bson_new_from_json is correct since it works using c directly). void foo() { import std.utf; import core.stdc.stdint; auto str_utf8 = "{\"a\":1}"; bson_error_t error; bson_new_from_json(cast(uint8_t*)str_utf8, (cast(uint8_t[])str_utf8).length, &error); } Re -1 in the call: Apparently it uses strlen() to deduce the size. However, I tried explicitly state the array length but had no luck.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? ThanksI 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.
Jun 30 2020
bson_t* bson_new_from_json(in char* data, long len, bson_error_t* error); string str_utf8 = "{\"a\":1}"; bson_error_t error; auto bson = bson_new_from_json(str_utf8.ptr, str_utf8.length, &error); You have a wrong declaration for bson_error_t too.
Jun 30 2020
On Wed, Jul 01, 2020 at 05:04:28AM +0000, Anthony via Digitalmars-d-learn wrote: [...]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?D strings are generally not null-terminated (except for literals). Before passing them to a C function you need to add a trailing null. Try using std.conv.toStringz instead of casting the pointer yourself. T -- A programming language should be a toolbox for the programmer to draw upon, not a minefield of dangerous explosives that you have to very carefully avoid touching in the wrong way.
Jun 30 2020
On Wednesday, 1 July 2020 at 05:33:48 UTC, H. S. Teoh wrote:On Wed, Jul 01, 2020 at 05:04:28AM +0000, Anthony via Digitalmars-d-learn wrote: [...]Thanks H. S. Teoh. Hmm, still same result though. import std.string; auto str = toStringz("{\"a\":1}"); bson_error_t error; bson_new_from_json(str, -1, &error); extern(C) { ... bson_t* bson_new_from_json(const char* data, long len, bson_error_t* error); }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?D strings are generally not null-terminated (except for literals). Before passing them to a C function you need to add a trailing null. Try using std.conv.toStringz instead of casting the pointer yourself. T
Jun 30 2020
On Wednesday, 1 July 2020 at 05:47:16 UTC, Anthony wrote:On Wednesday, 1 July 2020 at 05:33:48 UTC, H. S. Teoh wrote:Noob mistake: I declared an array that should be of fixed size. struct bson_error_t { .... char[] message; }; Should be: struct bson_error_t { .... char[504] message; }; :/On Wed, Jul 01, 2020 at 05:04:28AM +0000, Anthony via Digitalmars-d-learn wrote: [...]Thanks H. S. Teoh. Hmm, still same result though. import std.string; auto str = toStringz("{\"a\":1}"); bson_error_t error; bson_new_from_json(str, -1, &error); extern(C) { ... bson_t* bson_new_from_json(const char* data, long len, bson_error_t* error); }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?D strings are generally not null-terminated (except for literals). Before passing them to a C function you need to add a trailing null. Try using std.conv.toStringz instead of casting the pointer yourself. T
Jun 30 2020