Skip to content

Commit 2c383af

Browse files
author
Vincent Rouillé
committed
fix unpack of ix that doesn't fix in ix
1 parent ca3170a commit 2c383af

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

foundationdb/src/tuple/mod.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,57 @@ mod tests {
347347
b"\x0C\x80\x00\x00\x00\x00\x00\x00\x00",
348348
);
349349
test_serde(i64::min_value(), b"\x0C\x7f\xff\xff\xff\xff\xff\xff\xff");
350+
351+
test_serde(9252427359321063944i128, b"\x1c\x80g9\xa9np\x02\x08");
352+
assert!(matches!(
353+
unpack::<i64>(b"\x1c\x80g9\xa9np\x02\x08").unwrap_err(),
354+
PackError::UnsupportedIntLength
355+
));
356+
357+
test_serde(
358+
-9252427359321063944i128,
359+
b"\x0c\x7f\x98\xc6V\x91\x8f\xfd\xf7",
360+
);
361+
assert!(matches!(
362+
unpack::<i64>(b"\x0c\x7f\x98\xc6V\x91\x8f\xfd\xf7").unwrap_err(),
363+
PackError::UnsupportedIntLength
364+
));
365+
366+
test_serde(
367+
u64::max_value() as i128,
368+
b"\x1c\xff\xff\xff\xff\xff\xff\xff\xff",
369+
);
370+
assert!(matches!(
371+
unpack::<i64>(b"\x1c\xff\xff\xff\xff\xff\xff\xff\xff").unwrap_err(),
372+
PackError::UnsupportedIntLength
373+
));
374+
375+
test_serde(
376+
-(u64::max_value() as i128),
377+
b"\x0c\x00\x00\x00\x00\x00\x00\x00\x00",
378+
);
379+
assert!(matches!(
380+
unpack::<i64>(b"\x0c\x00\x00\x00\x00\x00\x00\x00\x00").unwrap_err(),
381+
PackError::UnsupportedIntLength
382+
));
383+
384+
test_serde(
385+
(i64::max_value() as i128) + 1,
386+
b"\x1c\x80\x00\x00\x00\x00\x00\x00\x00",
387+
);
388+
assert!(matches!(
389+
unpack::<i64>(b"\x1c\x80\x00\x00\x00\x00\x00\x00\x00").unwrap_err(),
390+
PackError::UnsupportedIntLength
391+
));
392+
393+
test_serde(
394+
(i64::min_value() as i128) - 1,
395+
b"\x0c\x7f\xff\xff\xff\xff\xff\xff\xfe",
396+
);
397+
assert!(matches!(
398+
unpack::<i64>(b"\x0c\x7f\xff\xff\xff\xff\xff\xff\xfe").unwrap_err(),
399+
PackError::UnsupportedIntLength
400+
));
350401
}
351402

352403
#[cfg(feature = "num-bigint")]
@@ -478,6 +529,15 @@ mod tests {
478529
BigInt::from(i64::min_value()),
479530
b"\x0C\x7f\xff\xff\xff\xff\xff\xff\xff",
480531
);
532+
533+
test_serde(
534+
Element::BigInt(9252427359321063944i128.into()),
535+
b"\x1c\x80g9\xa9np\x02\x08",
536+
);
537+
test_serde(
538+
Element::BigInt((-9252427359321063944i128).into()),
539+
b"\x0c\x7f\x98\xc6V\x91\x8f\xfd\xf7",
540+
);
481541
}
482542

483543
#[cfg(feature = "uuid")]

foundationdb/src/tuple/pack.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,39 @@ macro_rules! sign_bit {
313313
};
314314
}
315315

316-
macro_rules! unpack_px {
316+
macro_rules! unpack_ux {
317317
($ux: ident, $input: expr, $n: expr) => {{
318318
let (input, bytes) = parse_bytes($input, $n)?;
319319
let mut arr = [0u8; ::std::mem::size_of::<$ux>()];
320320
(&mut arr[(::std::mem::size_of::<$ux>() - $n)..]).copy_from_slice(bytes);
321321
(input, $ux::from_be_bytes(arr))
322322
}};
323323
}
324+
325+
macro_rules! unpack_px {
326+
($ix: ident, $ux: ident, $input: expr, $n: expr) => {{
327+
let (input, bytes) = parse_bytes($input, $n)?;
328+
let mut arr = [0u8; ::std::mem::size_of::<$ux>()];
329+
(&mut arr[(::std::mem::size_of::<$ux>() - $n)..]).copy_from_slice(bytes);
330+
let x = $ix::from_be_bytes(arr);
331+
if x < 0 {
332+
Err(PackError::UnsupportedIntLength)
333+
} else {
334+
Ok((input, x))
335+
}
336+
}};
337+
}
324338
macro_rules! unpack_nx {
325-
($ix: ident, $input: expr, $n: expr) => {{
339+
($ix: ident, $ux: ident, $input: expr, $n: expr) => {{
326340
let (input, bytes) = parse_bytes($input, $n)?;
327341
let mut arr = [0xffu8; ::std::mem::size_of::<$ix>()];
328342
(&mut arr[(::std::mem::size_of::<$ix>() - $n)..]).copy_from_slice(bytes);
329-
(input, $ix::from_be_bytes(arr).wrapping_add(1))
343+
let x = $ix::from_be_bytes(arr).wrapping_add(1);
344+
if x > 0 {
345+
Err(PackError::UnsupportedIntLength)
346+
} else {
347+
Ok((input, x))
348+
}
330349
}};
331350
}
332351

@@ -363,14 +382,14 @@ macro_rules! impl_ux {
363382
let (input, found) = parse_byte(input)?;
364383
if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
365384
let n = (found - INTZERO) as usize;
366-
Ok(unpack_px!($ux, input, n))
385+
Ok(unpack_ux!($ux, input, n))
367386
} else if found == POSINTEND {
368387
let (input, raw_length) = parse_byte(input)?;
369388
let n: usize = usize::from(raw_length);
370389
if n > SZ {
371390
return Err(PackError::UnsupportedIntLength);
372391
}
373-
Ok(unpack_px!($ux, input, n))
392+
Ok(unpack_ux!($ux, input, n))
374393
} else {
375394
Err(PackError::BadCode {
376395
found,
@@ -427,24 +446,24 @@ macro_rules! impl_ix {
427446
let (input, found) = parse_byte(input)?;
428447
if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
429448
let n = (found - INTZERO) as usize;
430-
Ok(unpack_px!($ix, input, n))
449+
unpack_px!($ix, $ux, input, n)
431450
} else if INTZERO - $max_sz as u8 <= found && found < INTZERO {
432451
let n = (INTZERO - found) as usize;
433-
Ok(unpack_nx!($ix, input, n))
452+
unpack_nx!($ix, $ux, input, n)
434453
} else if found == NEGINTSTART {
435454
let (input, raw_length) = parse_byte(input)?;
436455
let n = usize::from(raw_length ^ 0xff);
437456
if n > SZ {
438457
return Err(PackError::UnsupportedIntLength);
439458
}
440-
Ok(unpack_nx!($ix, input, n))
459+
unpack_nx!($ix, $ux, input, n)
441460
} else if found == POSINTEND {
442461
let (input, raw_length) = parse_byte(input)?;
443462
let n: usize = usize::from(raw_length);
444463
if n > SZ {
445464
return Err(PackError::UnsupportedIntLength);
446465
}
447-
Ok(unpack_px!($ix, input, n))
466+
unpack_px!($ix, $ux, input, n)
448467
} else {
449468
Err(PackError::BadCode {
450469
found,
@@ -961,10 +980,15 @@ impl<'de> TupleUnpack<'de> for Element<'de> {
961980
let (input, v) = Vec::<Self>::unpack(input, tuple_depth)?;
962981
(input, Element::Tuple(v))
963982
}
964-
INTMIN..=INTMAX => {
965-
let (input, v) = i64::unpack(input, tuple_depth)?;
966-
(input, Element::Int(v))
967-
}
983+
INTMIN..=INTMAX => match i64::unpack(input, tuple_depth) {
984+
Ok((input, v)) => (input, Element::Int(v)),
985+
#[cfg(feature = "num-bigint")]
986+
Err(PackError::UnsupportedIntLength) => {
987+
let (input, v) = num_bigint::BigInt::unpack(input, tuple_depth)?;
988+
(input, Element::BigInt(v))
989+
}
990+
Err(err) => return Err(err),
991+
},
968992
#[cfg(feature = "num-bigint")]
969993
NEGINTSTART => {
970994
let (input, v) = num_bigint::BigInt::unpack(input, tuple_depth)?;

0 commit comments

Comments
 (0)