From 0f2c937895dced6816d95cd73f413e6dd8749553 Mon Sep 17 00:00:00 2001 From: treeform Date: Fri, 20 May 2022 18:12:13 -0700 Subject: [PATCH] readStr and fuzzing. --- pixie.nimble | 2 +- src/pixie/fileformats/jpeg.nim | 12 +++++++++--- src/pixie/images.nim | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pixie.nimble b/pixie.nimble index 470b9f9..dc17c54 100644 --- a/pixie.nimble +++ b/pixie.nimble @@ -9,7 +9,7 @@ requires "nim >= 1.4.8" requires "vmath >= 1.1.4" requires "chroma >= 0.2.5" requires "zippy >= 0.9.7" -requires "flatty >= 0.3.2" +requires "flatty >= 0.3.3" requires "nimsimd >= 1.0.0" requires "bumpy >= 1.1.1" diff --git a/src/pixie/fileformats/jpeg.nim b/src/pixie/fileformats/jpeg.nim index 3cafa48..a85514a 100644 --- a/src/pixie/fileformats/jpeg.nim +++ b/src/pixie/fileformats/jpeg.nim @@ -119,6 +119,13 @@ proc readUint32be(state: var DecoderState): uint32 = (state.readUint8().uint32 shl 8) or state.readUint8().uint32 +proc readStr(state: var DecoderState, n: int): string = + ## Reads n number of bytes as a string. + if state.pos + n > state.buffer.len: + failInvalid() + result = state.buffer[state.pos ..< state.pos + n] + state.pos += n + proc skipBytes(state: var DecoderState, n: int) = ## Skips a number of bytes. if state.pos + n > state.buffer.len: @@ -329,8 +336,7 @@ proc decodeExif(state: var DecoderState) = len = state.readUint16be().int - 2 endOffset = state.pos + len - let exifHeader = state.buffer[state.pos ..< state.pos + 6] - state.skipBytes(6) + let exifHeader = state.readStr(6) if exifHeader != "Exif\0\0": # Happens with progressive images, just ignore instead of error. # Skip to the end. @@ -519,7 +525,7 @@ proc getBit(state: var DecoderState): int = proc getBitsAsSignedInt(state: var DecoderState, n: int): int = ## Get n number of bits as a signed integer. - if n notin 0 .. 16: + if n notin 0 .. 15: failInvalid() if state.bitsBuffered < n: state.fillBitBuffer() diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 7ff8d45..a5fb534 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -172,7 +172,7 @@ proc flipVertical*(image: Image) {.raises: [].} = proc rotate90*(image: Image) {.raises: [PixieError].} = ## Rotates the image 90 degrees clockwise. - var copy = newImage(image.height, image.width) + let copy = newImage(image.height, image.width) for y in 0 ..< copy.height: for x in 0 ..< copy.width: copy.data[copy.dataIndex(x, y)] =