Skip to content

Commit

Permalink
refactor: Use snprintf instead of sprintf for creating ISO time string
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Jun 3, 2024
1 parent 248e0e4 commit 20b80fb
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pkcs11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ napi_value create_date_utc_property(napi_env env, CK_UTF8CHAR_PTR utcTime)
napi_get_named_property(env, utcTimeConstructor, "Date", &utcTimeConstructor);
napi_value utcTimeDate;

if (utcTime == nullptr || utcTime[0] == '\0' || utcTime[0] == ' ')
if (utcTime == nullptr || utcTime[0] == '\0' || utcTime[0] == ' ' || strlen((char *)utcTime) < 14)
{
napi_new_instance(env, utcTimeConstructor, 0, nullptr, &utcTimeDate);
return utcTimeDate;
}

// create string from utcTime
char isoTime[20];
sprintf(isoTime, "%c%c%c%c-%c%c-%c%cT%c%c:%c%c:%c%cZ", utcTime[0], utcTime[1], utcTime[2], utcTime[3], utcTime[4], utcTime[5], utcTime[6], utcTime[7], utcTime[8], utcTime[9], utcTime[10], utcTime[11], utcTime[12], utcTime[13]);
snprintf(isoTime, sizeof(isoTime), "%c%c%c%c-%c%c-%c%cT%c%c:%c%c:%c%cZ", utcTime[0], utcTime[1], utcTime[2], utcTime[3], utcTime[4], utcTime[5], utcTime[6], utcTime[7], utcTime[8], utcTime[9], utcTime[10], utcTime[11], utcTime[12], utcTime[13]);
napi_value utcTimeValue;
napi_create_string_utf8(env, isoTime, NAPI_AUTO_LENGTH, &utcTimeValue);

Expand Down

0 comments on commit 20b80fb

Please sign in to comment.