From 037aa20b3725190d42cf27bec5e143da2b781c84 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 25 Nov 2023 22:49:46 +0100 Subject: [PATCH] QPpmHandler: fix reading ppm files The ppm spec allows 1-bit ascii ppm files to have no spaces bitween the single bits. Therefore we have to adjust read_pbm_int() to stop after reading one digit. Fixes: QTBUG-119239 Change-Id: I161038076c5dee4662aa66a1215822fc75e6a19e Reviewed-by: Eirik Aavitsland (cherry picked from commit 975b3d5d530b647ba665459bef2fb3ee6ab5ccc1) Reviewed-by: Qt Cherry-pick Bot --- src/gui/image/qppmhandler.cpp | 8 ++++++-- tests/auto/gui/image/qimagereader/images/image.pbm | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/gui/image/qppmhandler.cpp b/src/gui/image/qppmhandler.cpp index e53db69e665..d894f21d5c9 100644 --- a/src/gui/image/qppmhandler.cpp +++ b/src/gui/image/qppmhandler.cpp @@ -32,7 +32,7 @@ static void discard_pbm_line(QIODevice *d) } while (res > 0 && buf[res-1] != '\n'); } -static int read_pbm_int(QIODevice *d, bool *ok) +static int read_pbm_int(QIODevice *d, bool *ok, int maxDigits = -1) { char c; int val = -1; @@ -50,6 +50,8 @@ static int read_pbm_int(QIODevice *d, bool *ok) } else { hasOverflow = true; } + if (maxDigits > 0 && --maxDigits == 0) + break; continue; } else { if (c == '#') // comment @@ -65,6 +67,8 @@ static int read_pbm_int(QIODevice *d, bool *ok) discard_pbm_line(d); else break; + if (maxDigits > 0 && --maxDigits == 0) + break; } if (val < 0) *ok = false; @@ -213,7 +217,7 @@ static bool read_pbm_body(QIODevice *device, char type, int w, int h, int mcc, Q b = 0; for (int i=0; i<8; i++) { if (i < bitsLeft) - b = (b << 1) | (read_pbm_int(device, &ok) & 1); + b = (b << 1) | (read_pbm_int(device, &ok, 1) & 1); else b = (b << 1) | (0 & 1); // pad it our self if we need to } diff --git a/tests/auto/gui/image/qimagereader/images/image.pbm b/tests/auto/gui/image/qimagereader/images/image.pbm index 67e5efa3e9c..e529536ca48 100644 --- a/tests/auto/gui/image/qimagereader/images/image.pbm +++ b/tests/auto/gui/image/qimagereader/images/image.pbm @@ -1,8 +1,8 @@ P1 16 6 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 +10 00 00 00 01 00 00 01 +1000 0000 0100 0001 +100000000 1000001 +1000000001000001 +10 000 0000 10000 01