Skip to content

Commit

Permalink
Merge pull request #13029 from Freezystem/patch-2
Browse files Browse the repository at this point in the history
Prevent casting null or undefined UUID field value
  • Loading branch information
vkarpov15 committed Feb 15, 2023
2 parents 8e5bbfa + 2f9c822 commit 06585a8
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Binary = MongooseBuffer.Binary;

function hex2buffer(hex) {
// use buffer built-in function to convert from hex-string to buffer
const buff = Buffer.from(hex, 'hex');
const buff = hex && Buffer.from(hex, 'hex');
return buff;
}

Expand All @@ -36,7 +36,7 @@ function hex2buffer(hex) {

function binary2hex(buf) {
// use buffer built-in function to convert from buffer to hex-string
const hex = buf.toString('hex');
const hex = buf && buf.toString('hex');
return hex;
}

Expand Down Expand Up @@ -66,9 +66,8 @@ function stringToBinary(uuidStr) {
*/
function binaryToString(uuidBin) {
// i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
let hex;
if (typeof uuidBin !== 'string') {
hex = binary2hex(uuidBin);
if (uuidBin && typeof uuidBin !== 'string') {
const hex = binary2hex(uuidBin);
const uuidStr = hex.substring(0, 8) + '-' + hex.substring(8, 8 + 4) + '-' + hex.substring(12, 12 + 4) + '-' + hex.substring(16, 16 + 4) + '-' + hex.substring(20, 20 + 12);
return uuidStr;
}
Expand Down

0 comments on commit 06585a8

Please sign in to comment.