Skip to content

Commit 1af5f35

Browse files
committed
Fix attempt to subtract with overflow
1 parent 16c01b8 commit 1af5f35

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/qrcode/decoder/version.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ impl Version {
146146
*
147147
* @param dimension dimension in modules
148148
* @return Version for a QR Code of that dimension
149-
* @throws FormatException if dimension is not 1 mod 4
149+
* @throws FormatException if dimension is not 1 mod 4 or dimension less than 17
150150
*/
151151
pub fn getProvisionalVersionForDimension(dimension: u32) -> Result<VersionRef> {
152-
if dimension % 4 != 1 {
152+
if dimension % 4 != 1 || dimension < 17 {
153153
return Err(Exceptions::format_with("dimension incorrect"));
154154
}
155155
Self::getVersionForNumber((dimension - 17) / 4)

src/qrcode/detector/qrcode_detector.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,30 @@ impl<'a> Detector<'_> {
429429
let allowance = (allowanceFactor * overallEstModuleSize) as u32;
430430
let alignmentAreaLeftX = 0.max(estAlignmentX as i32 - allowance as i32) as u32;
431431
let alignmentAreaRightX = (self.image.getWidth() - 1).min(estAlignmentX + allowance);
432-
if ((alignmentAreaRightX - alignmentAreaLeftX) as f32) < overallEstModuleSize * 3.0 {
432+
let alignmentAreaWidth = alignmentAreaRightX
433+
.checked_sub(alignmentAreaLeftX)
434+
.ok_or(Exceptions::NOT_FOUND)?;
435+
436+
if (alignmentAreaWidth as f32) < overallEstModuleSize * 3.0 {
433437
return Err(Exceptions::NOT_FOUND);
434438
}
435439

436440
let alignmentAreaTopY = 0.max(estAlignmentY as i32 - allowance as i32) as u32;
437441
let alignmentAreaBottomY = (self.image.getHeight() - 1).min(estAlignmentY + allowance);
438-
if alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize as u32 * 3 {
442+
let alignmentAreaHeight = alignmentAreaTopY
443+
.checked_sub(alignmentAreaBottomY)
444+
.ok_or(Exceptions::NOT_FOUND)?;
445+
446+
if alignmentAreaHeight < overallEstModuleSize as u32 * 3 {
439447
return Err(Exceptions::NOT_FOUND);
440448
}
441449

442450
let mut alignmentFinder = AlignmentPatternFinder::new(
443451
self.image,
444452
alignmentAreaLeftX,
445453
alignmentAreaTopY,
446-
alignmentAreaRightX - alignmentAreaLeftX,
447-
alignmentAreaBottomY - alignmentAreaTopY,
454+
alignmentAreaWidth,
455+
alignmentAreaHeight,
448456
overallEstModuleSize,
449457
self.resultPointCallback.clone(),
450458
);

0 commit comments

Comments
 (0)