json-maker icon indicating copy to clipboard operation
json-maker copied to clipboard

error: too few arguments to function 'json_objOpen'

Open smdjeff opened this issue 3 years ago • 2 comments

prototype in json-maker.h is char* json_objOpen( char* dest, char const* name, size_t* remLen );

but the sample code is... dest = json_objOpen( dest, name );

also noticed the code looked suspiciously reactionary to buffer overflow at...

   char buff[512];
    int len = data_to_json( buff, &data );
    if( len >= sizeof buff ) {
        fprintf( stderr, "%s%d%s%d\n", "Error. Len: ", len, " Max: ", (int)sizeof buff - 1 );
        return EXIT_FAILURE;
    }

betting that issue was fixed, but the sample code wasn't updated.

smdjeff avatar Nov 17 '21 18:11 smdjeff

sample code

static size_t remLen; // MAX JSON String Count

struct header { int protocolVersion; int messageId; int stationId; }; struct data { struct header head; };

char* json_header( char* dest, char const* name, struct header const* head ) { dest = json_objOpen( dest, name, &remLen ); dest = json_int( dest, "protocolVersion", head->protocolVersion, &remLen ); dest = json_int( dest, "messageId", head->messageId, &remLen ); dest = json_objOpen( dest, "stationId", &remLen ); dest = json_int( dest, "value", head->stationId, &remLen ); dest = json_objClose( dest, &remLen ); dest = json_objClose( dest, &remLen ); return dest; } char* json_data( char* dest, char const* name, struct data const* data ) { dest = json_objOpen( dest, NULL, &remLen ); dest = json_header( dest, "header", &data->head ); dest = json_objClose( dest, &remLen ); return dest; } int data_to_json( char* dest, struct data const* data ) { char* p = json_data( dest, NULL, data, &remLen ); p = json_end( p, &remLen ); return p - dest; }

void main() { static struct data const data = { .head = { .protocolVersion = 1, .messageId = 2, .stationId = 185 } } char buff[512]; remLen = sizeof(buff); int len = data_to_json( buff, &data ); printf("%s \n", buff); return; }

result {"header":{"protocolVersion":1,"messageId":2,"stationId":{"value":185}}}

woogi1368 avatar Aug 24 '22 01:08 woogi1368

Seconded, @woogi1368's sample code fixes this issue and works, just tested it

CoffeePanda0 avatar Aug 30 '22 12:08 CoffeePanda0