Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

error: too few arguments to function 'json_objOpen' #8

Open
smdjeff opened this issue Nov 17, 2021 · 2 comments
Open

error: too few arguments to function 'json_objOpen' #8

smdjeff opened this issue Nov 17, 2021 · 2 comments

Comments

@smdjeff
Copy link

smdjeff commented Nov 17, 2021

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.

@woogi1368
Copy link

woogi1368 commented Aug 24, 2022

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}}}

@CoffeePanda0
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants