Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor cgltf_json_to_int(), cgltf_json_to_size(), cgltf_json_to_float() without strncpy() #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2711,32 +2711,37 @@ static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, co
static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
char tmp[128];
int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
return CGLTF_ATOI(tmp);
int size = (int)(tok->end - tok->start);
uint8_t* ptr = (uint8_t*)json_chunk + tok->start + size;
const uint8_t tmp = *ptr;
*ptr = 0;
int res = CGLTF_ATOI((const char *)(json_chunk + tok->start));
*ptr = tmp;
return res;
}

static cgltf_size cgltf_json_to_size(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE_RET(*tok, JSMN_PRIMITIVE, 0);
char tmp[128];
int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
long long res = CGLTF_ATOLL(tmp);
int size = (int)(tok->end - tok->start);
uint8_t* ptr = (uint8_t*)json_chunk + tok->start + size;
const uint8_t tmp = *ptr;
*ptr = 0;
long long res = CGLTF_ATOLL((const char *)(json_chunk + tok->start));
*ptr = tmp;
return res < 0 ? 0 : (cgltf_size)res;
}

static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
char tmp[128];
int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
return (cgltf_float)CGLTF_ATOF(tmp);
int size = (int)(tok->end - tok->start);
uint8_t* ptr = (uint8_t*)json_chunk + tok->start + size;
const uint8_t tmp = *ptr;
*ptr = 0;
cgltf_float res = (cgltf_float)CGLTF_ATOF((const char *)(json_chunk + tok->start));
*ptr = tmp;
return res;
}

static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
Expand Down