Skip to content

Commit 9e2c5fd

Browse files
legendecasaduh95
authored andcommitted
src: simply uint32 to string as it must not fail
PR-URL: #60846 Refs: #53959 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ab18c08 commit 9e2c5fd

File tree

3 files changed

+25
-75
lines changed

3 files changed

+25
-75
lines changed

src/node_contextify.cc

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ using v8::ScriptCompiler;
8585
using v8::ScriptOrigin;
8686
using v8::String;
8787
using v8::Symbol;
88-
using v8::Uint32;
8988
using v8::UnboundScript;
9089
using v8::Value;
9190

@@ -109,15 +108,6 @@ using v8::Value;
109108
// For every `set` of a global property, the interceptor callback defines or
110109
// changes the property both on the sandbox and the global proxy.
111110

112-
namespace {
113-
114-
// Convert an int to a V8 Name (String or Symbol).
115-
MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
116-
return Uint32::New(context->GetIsolate(), index)->ToString(context);
117-
}
118-
119-
} // anonymous namespace
120-
121111
ContextifyContext* ContextifyContext::New(Environment* env,
122112
Local<Object> sandbox_obj,
123113
ContextOptions* options) {
@@ -845,11 +835,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback(
845835
return Intercepted::kNo;
846836
}
847837

848-
Local<String> name;
849-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
850-
return ContextifyContext::PropertyQueryCallback(name, args);
851-
}
852-
return Intercepted::kNo;
838+
Local<String> name = Uint32ToString(ctx->context(), index);
839+
return ContextifyContext::PropertyQueryCallback(name, args);
853840
}
854841

855842
// static
@@ -862,11 +849,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback(
862849
return Intercepted::kNo;
863850
}
864851

865-
Local<String> name;
866-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
867-
return ContextifyContext::PropertyGetterCallback(name, args);
868-
}
869-
return Intercepted::kNo;
852+
Local<String> name = Uint32ToString(ctx->context(), index);
853+
return ContextifyContext::PropertyGetterCallback(name, args);
870854
}
871855

872856
Intercepted ContextifyContext::IndexedPropertySetterCallback(
@@ -880,11 +864,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback(
880864
return Intercepted::kNo;
881865
}
882866

883-
Local<String> name;
884-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
885-
return ContextifyContext::PropertySetterCallback(name, value, args);
886-
}
887-
return Intercepted::kNo;
867+
Local<String> name = Uint32ToString(ctx->context(), index);
868+
return ContextifyContext::PropertySetterCallback(name, value, args);
888869
}
889870

890871
// static
@@ -897,11 +878,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
897878
return Intercepted::kNo;
898879
}
899880

900-
Local<String> name;
901-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
902-
return ContextifyContext::PropertyDescriptorCallback(name, args);
903-
}
904-
return Intercepted::kNo;
881+
Local<String> name = Uint32ToString(ctx->context(), index);
882+
return ContextifyContext::PropertyDescriptorCallback(name, args);
905883
}
906884

907885
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
@@ -915,11 +893,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
915893
return Intercepted::kNo;
916894
}
917895

918-
Local<String> name;
919-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
920-
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
921-
}
922-
return Intercepted::kNo;
896+
Local<String> name = Uint32ToString(ctx->context(), index);
897+
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
923898
}
924899

925900
// static

src/node_webstorage.cc

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ using v8::PropertyCallbackInfo;
4141
using v8::PropertyDescriptor;
4242
using v8::PropertyHandlerFlags;
4343
using v8::String;
44-
using v8::Uint32;
4544
using v8::Value;
4645

4746
#define THROW_SQLITE_ERROR(env, r) \
@@ -436,10 +435,6 @@ Maybe<void> Storage::Store(Local<Name> key, Local<Value> value) {
436435
return JustVoid();
437436
}
438437

439-
static MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
440-
return Uint32::New(context->GetIsolate(), index)->ToString(context);
441-
}
442-
443438
static void Clear(const FunctionCallbackInfo<Value>& info) {
444439
Storage* storage;
445440
ASSIGN_OR_RETURN_UNWRAP(&storage, info.This());
@@ -635,67 +630,37 @@ static Intercepted StorageDefiner(Local<Name> property,
635630
static Intercepted IndexedGetter(uint32_t index,
636631
const PropertyCallbackInfo<Value>& info) {
637632
Environment* env = Environment::GetCurrent(info);
638-
Local<Name> name;
639-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
640-
// There was an error converting the index to a name.
641-
// We aren't going to return a result but let's indicate
642-
// that we intercepted the operation.
643-
return Intercepted::kYes;
644-
}
633+
Local<Name> name = Uint32ToString(env->context(), index);
645634
return StorageGetter(name, info);
646635
}
647636

648637
static Intercepted IndexedSetter(uint32_t index,
649638
Local<Value> value,
650639
const PropertyCallbackInfo<void>& info) {
651640
Environment* env = Environment::GetCurrent(info);
652-
Local<Name> name;
653-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
654-
// There was an error converting the index to a name.
655-
// We aren't going to return a result but let's indicate
656-
// that we intercepted the operation.
657-
return Intercepted::kYes;
658-
}
641+
Local<Name> name = Uint32ToString(env->context(), index);
659642
return StorageSetter(name, value, info);
660643
}
661644

662645
static Intercepted IndexedQuery(uint32_t index,
663646
const PropertyCallbackInfo<Integer>& info) {
664647
Environment* env = Environment::GetCurrent(info);
665-
Local<Name> name;
666-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
667-
// There was an error converting the index to a name.
668-
// We aren't going to return a result but let's indicate
669-
// that we intercepted the operation.
670-
return Intercepted::kYes;
671-
}
648+
Local<Name> name = Uint32ToString(env->context(), index);
672649
return StorageQuery(name, info);
673650
}
674651

675652
static Intercepted IndexedDeleter(uint32_t index,
676653
const PropertyCallbackInfo<Boolean>& info) {
677654
Environment* env = Environment::GetCurrent(info);
678-
Local<Name> name;
679-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
680-
// There was an error converting the index to a name.
681-
// We aren't going to return a result but let's indicate
682-
// that we intercepted the operation.
683-
return Intercepted::kYes;
684-
}
655+
Local<Name> name = Uint32ToString(env->context(), index);
685656
return StorageDeleter(name, info);
686657
}
687658

688659
static Intercepted IndexedDefiner(uint32_t index,
689660
const PropertyDescriptor& desc,
690661
const PropertyCallbackInfo<void>& info) {
691662
Environment* env = Environment::GetCurrent(info);
692-
Local<Name> name;
693-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
694-
// There was an error converting the index to a name.
695-
// We aren't going to return a result but let's indicate
696-
// that we intercepted the operation.
697-
return Intercepted::kYes;
698-
}
663+
Local<Name> name = Uint32ToString(env->context(), index);
699664
return StorageDefiner(name, desc, info);
700665
}
701666

src/util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,16 @@ inline v8::MaybeLocal<v8::Object> NewDictionaryInstanceNullProto(
10631063
v8::Local<v8::DictionaryTemplate> tmpl,
10641064
v8::MemorySpan<v8::MaybeLocal<v8::Value>> property_values);
10651065

1066+
// Convert an uint32 to a V8 String.
1067+
inline v8::Local<v8::String> Uint32ToString(v8::Local<v8::Context> context,
1068+
uint32_t index) {
1069+
// V8 internally caches strings for small integers, and asserts that a
1070+
// non-empty string local handle is returned for `ToString`.
1071+
return v8::Uint32::New(v8::Isolate::GetCurrent(), index)
1072+
->ToString(context)
1073+
.ToLocalChecked();
1074+
}
1075+
10661076
} // namespace node
10671077

10681078
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)