Skip to content

Commit 06c018c

Browse files
committed
src: refactor to use THROW_* argument based snprintf
1 parent 4243ce0 commit 06c018c

File tree

7 files changed

+35
-58
lines changed

7 files changed

+35
-58
lines changed

src/crypto/crypto_cipher.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ bool CipherBase::InitAuthenticated(
511511
if (mode == EVP_CIPH_GCM_MODE) {
512512
if (auth_tag_len != kNoAuthTagLength) {
513513
if (!IsValidGCMTagLength(auth_tag_len)) {
514-
char msg[50];
515-
snprintf(msg, sizeof(msg),
516-
"Invalid authentication tag length: %u", auth_tag_len);
517-
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env(), msg);
514+
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
515+
env(),
516+
"Invalid authentication tag length: %u",
517+
auth_tag_len);
518518
return false;
519519
}
520520

@@ -523,9 +523,8 @@ bool CipherBase::InitAuthenticated(
523523
}
524524
} else {
525525
if (auth_tag_len == kNoAuthTagLength) {
526-
char msg[128];
527-
snprintf(msg, sizeof(msg), "authTagLength required for %s", cipher_type);
528-
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env(), msg);
526+
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
527+
env(), "authTagLength required for %s", cipher_type);
529528
return false;
530529
}
531530

@@ -633,10 +632,8 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
633632
}
634633

635634
if (!is_valid) {
636-
char msg[50];
637-
snprintf(msg, sizeof(msg),
638-
"Invalid authentication tag length: %u", tag_len);
639-
return THROW_ERR_CRYPTO_INVALID_AUTH_TAG(env, msg);
635+
return THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
636+
env, "Invalid authentication tag length: %u", tag_len);
640637
}
641638

642639
cipher->auth_tag_len_ = tag_len;

src/crypto/crypto_dsa.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ Maybe<bool> DsaKeyGenTraits::AdditionalConfig(
8484
params->params.modulus_bits = args[*offset].As<Uint32>()->Value();
8585
params->params.divisor_bits = args[*offset + 1].As<Int32>()->Value();
8686
if (params->params.divisor_bits < -1) {
87-
char msg[1024];
88-
snprintf(msg, sizeof(msg), "invalid value for divisor_bits");
89-
THROW_ERR_OUT_OF_RANGE(env, msg);
87+
THROW_ERR_OUT_OF_RANGE(env, "invalid value for divisor_bits");
9088
return Nothing<bool>();
9189
}
9290

src/crypto/crypto_hash.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ Maybe<bool> HashTraits::AdditionalConfig(
233233
Utf8Value digest(env->isolate(), args[offset]);
234234
params->digest = EVP_get_digestbyname(*digest);
235235
if (UNLIKELY(params->digest == nullptr)) {
236-
char msg[1024];
237-
snprintf(msg, sizeof(msg), "Invalid digest: %s", *digest);
238-
THROW_ERR_CRYPTO_INVALID_DIGEST(env);
236+
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest);
239237
return Nothing<bool>();
240238
}
241239

src/crypto/crypto_keys.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,7 @@ std::shared_ptr<KeyObjectData> ImportJWKAsymmetricKey(
515515
return ImportJWKEcKey(env, jwk, args, offset);
516516
}
517517

518-
char msg[1024];
519-
snprintf(msg, sizeof(msg), "%s is not a supported JWK key type", kty);
520-
THROW_ERR_CRYPTO_INVALID_JWK(env, msg);
518+
THROW_ERR_CRYPTO_INVALID_JWK(env, "%s is not a supported JWK key type", kty);
521519
return std::shared_ptr<KeyObjectData>();
522520
}
523521

src/crypto/crypto_rsa.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ Maybe<bool> RsaKeyGenTraits::AdditionalConfig(
138138
Utf8Value digest(env->isolate(), args[*offset]);
139139
params->params.md = EVP_get_digestbyname(*digest);
140140
if (params->params.md == nullptr) {
141-
char msg[1024];
142-
snprintf(msg, sizeof(msg), "md specifies an invalid digest");
143-
THROW_ERR_CRYPTO_INVALID_DIGEST(env, msg);
141+
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "md specifies an invalid digest");
144142
return Nothing<bool>();
145143
}
146144
}
@@ -150,9 +148,8 @@ Maybe<bool> RsaKeyGenTraits::AdditionalConfig(
150148
Utf8Value digest(env->isolate(), args[*offset + 1]);
151149
params->params.mgf1_md = EVP_get_digestbyname(*digest);
152150
if (params->params.mgf1_md == nullptr) {
153-
char msg[1024];
154-
snprintf(msg, sizeof(msg), "mgf1_md specifies an invalid digest");
155-
THROW_ERR_CRYPTO_INVALID_DIGEST(env, msg);
151+
THROW_ERR_CRYPTO_INVALID_DIGEST(env,
152+
"mgf1_md specifies an invalid digest");
156153
return Nothing<bool>();
157154
}
158155
}
@@ -161,9 +158,9 @@ Maybe<bool> RsaKeyGenTraits::AdditionalConfig(
161158
CHECK(args[*offset + 2]->IsInt32());
162159
params->params.saltlen = args[*offset + 2].As<Int32>()->Value();
163160
if (params->params.saltlen < 0) {
164-
char msg[1024];
165-
snprintf(msg, sizeof(msg), "salt length is out of range");
166-
THROW_ERR_OUT_OF_RANGE(env, msg);
161+
THROW_ERR_OUT_OF_RANGE(
162+
env,
163+
"salt length is out of range");
167164
return Nothing<bool>();
168165
}
169166
}

src/crypto/crypto_scrypt.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ Maybe<bool> ScryptTraits::AdditionalConfig(
111111

112112
params->length = args[offset + 6].As<Int32>()->Value();
113113
if (params->length < 0) {
114-
char msg[1024];
115-
snprintf(msg, sizeof(msg), "length must be <= %d", INT_MAX);
116-
THROW_ERR_OUT_OF_RANGE(env, msg);
114+
THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX);
117115
return Nothing<bool>();
118116
}
119117

@@ -151,4 +149,3 @@ bool ScryptTraits::DeriveBits(
151149

152150
} // namespace crypto
153151
} // namespace node
154-

src/node_binding.cc

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,9 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
482482
mp = dlib->GetSavedModuleFromGlobalHandleMap();
483483
if (mp == nullptr || mp->nm_context_register_func == nullptr) {
484484
dlib->Close();
485-
char errmsg[1024];
486-
snprintf(errmsg,
487-
sizeof(errmsg),
488-
"Module did not self-register: '%s'.",
489-
*filename);
490-
THROW_ERR_DLOPEN_FAILED(env, errmsg);
485+
THROW_ERR_DLOPEN_FAILED(
486+
env, "Module did not self-register: '%s'.",
487+
*filename);
491488
return false;
492489
}
493490
}
@@ -502,23 +499,21 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
502499
callback(exports, module, context);
503500
return true;
504501
}
505-
char errmsg[1024];
506-
snprintf(errmsg,
507-
sizeof(errmsg),
508-
"The module '%s'"
509-
"\nwas compiled against a different Node.js version using"
510-
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
511-
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
512-
"re-installing\nthe module (for instance, using `npm rebuild` "
513-
"or `npm install`).",
514-
*filename,
515-
mp->nm_version,
516-
NODE_MODULE_VERSION);
517502

518503
// NOTE: `mp` is allocated inside of the shared library's memory, calling
519504
// `dlclose` will deallocate it
520505
dlib->Close();
521-
THROW_ERR_DLOPEN_FAILED(env, errmsg);
506+
THROW_ERR_DLOPEN_FAILED(
507+
env,
508+
"The module '%s'"
509+
"\nwas compiled against a different Node.js version using"
510+
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
511+
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
512+
"re-installing\nthe module (for instance, using `npm rebuild` "
513+
"or `npm install`).",
514+
*filename,
515+
mp->nm_version,
516+
NODE_MODULE_VERSION);
522517
return false;
523518
}
524519
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);
@@ -631,12 +626,9 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
631626
mod = FindModule(modlist_linked, name, NM_F_LINKED);
632627

633628
if (mod == nullptr) {
634-
char errmsg[1024];
635-
snprintf(errmsg,
636-
sizeof(errmsg),
637-
"No such module was linked: %s",
638-
*module_name_v);
639-
return THROW_ERR_INVALID_MODULE(env, errmsg);
629+
return THROW_ERR_INVALID_MODULE(env,
630+
"No such module was linked: %s",
631+
*module_name_v);
640632
}
641633

642634
Local<Object> module = Object::New(env->isolate());

0 commit comments

Comments
 (0)