diff options
author | 2022-08-03 21:31:15 -0700 | |
---|---|---|
committer | 2022-08-03 21:31:15 -0700 | |
commit | 3c5b68caa727477f603e93731a77ec035e7d3438 (patch) | |
tree | 525bf8adbe475ea94bfb0f53db7ab6e27335bb89 /src | |
parent | 37fe03e1ca731848cf144230a0e53c3fa1016201 (diff) | |
download | bun-3c5b68caa727477f603e93731a77ec035e7d3438.tar.gz bun-3c5b68caa727477f603e93731a77ec035e7d3438.tar.zst bun-3c5b68caa727477f603e93731a77ec035e7d3438.zip |
[node.js compat] Clamp for `Buffer` functions instead of throwing
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index 7cbd1faac..220d634f1 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -545,13 +545,8 @@ static inline JSC::EncodedJSValue jsBufferConstructorFunction_compareBody(JSC::J } } - if (targetStart > std::min(targetEnd, targetEndInit) || targetEnd > targetEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "targetStart and targetEnd out of range"_s)); - } - - if (sourceStart > std::min(sourceEnd, sourceEndInit) || sourceEnd > sourceEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "sourceStart and sourceEnd out of range"_s)); - } + targetStart = std::min(targetStart, std::min(targetEnd, targetEndInit)); + sourceStart = std::min(sourceStart, std::min(sourceEnd, sourceEndInit)); auto sourceLength = sourceEnd - sourceStart; auto targetLength = targetEnd - targetStart; @@ -763,13 +758,8 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_compareBody(JSC::JSG } } - if (targetStart > std::min(targetEnd, targetEndInit) || targetEnd > targetEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "targetStart and targetEnd out of range"_s)); - } - - if (sourceStart > std::min(sourceEnd, sourceEndInit) || sourceEnd > sourceEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "sourceStart and sourceEnd out of range"_s)); - } + targetStart = std::min(targetStart, std::min(targetEnd, targetEndInit)); + sourceStart = std::min(sourceStart, std::min(sourceEnd, sourceEndInit)); auto sourceLength = sourceEnd - sourceStart; auto targetLength = targetEnd - targetStart; @@ -852,13 +842,8 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob } } - if (targetStart > std::min(targetEnd, targetEndInit) || targetEnd > targetEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "targetStart and targetEnd out of range"_s)); - } - - if (sourceStart > std::min(sourceEnd, sourceEndInit) || sourceEnd > sourceEndInit) { - return throwVMError(lexicalGlobalObject, throwScope, createRangeError(lexicalGlobalObject, "sourceStart and sourceEnd out of range"_s)); - } + targetStart = std::min(targetStart, std::min(targetEnd, targetEndInit)); + sourceStart = std::min(sourceStart, std::min(sourceEnd, sourceEndInit)); auto sourceLength = sourceEnd - sourceStart; auto targetLength = targetEnd - targetStart; @@ -867,7 +852,8 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob auto sourceStartPtr = castedThis->typedVector() + sourceStart; auto targetStartPtr = view->typedVector() + targetStart; - memmove(targetStartPtr, sourceStartPtr, actualLength); + if (actualLength > 0) + memmove(targetStartPtr, sourceStartPtr, actualLength); return JSValue::encode(jsNumber(actualLength)); } |