Commit 3f68948c authored by Michael Niedermayer's avatar Michael Niedermayer

avcodec/pnm: Avoid structure pointer dereferences in inner loop in pnm_get()

Improves speed from 5.4 to 4.2 seconds
Fixes: 13149/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_PGM_fuzzer-5760833622114304
Fixes: 13166/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_PGMYUV_fuzzer-5763216322330624

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpegReviewed-by: 's avatarLauri Kasanen <cand@gmx.com>
Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 177b4089
...@@ -36,13 +36,15 @@ static void pnm_get(PNMContext *sc, char *str, int buf_size) ...@@ -36,13 +36,15 @@ static void pnm_get(PNMContext *sc, char *str, int buf_size)
{ {
char *s; char *s;
int c; int c;
uint8_t *bs = sc->bytestream;
const uint8_t *end = sc->bytestream_end;
/* skip spaces and comments */ /* skip spaces and comments */
while (sc->bytestream < sc->bytestream_end) { while (bs < end) {
c = *sc->bytestream++; c = *bs++;
if (c == '#') { if (c == '#') {
while (c != '\n' && sc->bytestream < sc->bytestream_end) { while (c != '\n' && bs < end) {
c = *sc->bytestream++; c = *bs++;
} }
} else if (!pnm_space(c)) { } else if (!pnm_space(c)) {
break; break;
...@@ -50,12 +52,13 @@ static void pnm_get(PNMContext *sc, char *str, int buf_size) ...@@ -50,12 +52,13 @@ static void pnm_get(PNMContext *sc, char *str, int buf_size)
} }
s = str; s = str;
while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) { while (bs < end && !pnm_space(c)) {
if ((s - str) < buf_size - 1) if ((s - str) < buf_size - 1)
*s++ = c; *s++ = c;
c = *sc->bytestream++; c = *bs++;
} }
*s = '\0'; *s = '\0';
sc->bytestream = bs;
} }
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s) int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment