From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 2/7] avcodec/sgidec: Support negative linesizes Date: Fri, 30 Sep 2022 19:05:10 +0200 Message-ID: <GV1P250MB0737584845B197233BFAF0208F569@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw) In-Reply-To: <GV1P250MB073747D27D2794CFC913647A8F569@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> The earlier code used "p->data[0] + p->linesize[0] * s->height" with the latter being unsigned, which gives the wrong value for negative linesizes. There is also a not so obvious problem with this: In case of negative linesizes, the last line is the start of the allocated buffer, so using the line after the last line would involve undefined pointer arithmetic. So don't do it. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/sgidec.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libavcodec/sgidec.c b/libavcodec/sgidec.c index dd3dc46b48..a449859bf8 100644 --- a/libavcodec/sgidec.c +++ b/libavcodec/sgidec.c @@ -28,7 +28,7 @@ typedef struct SgiState { AVCodecContext *avctx; unsigned int width; - unsigned int height; + int height; unsigned int depth; unsigned int bytes_per_channel; int linesize; @@ -127,12 +127,11 @@ static int expand_rle_row16(SgiState *s, uint16_t *out_buf, * @param s the current image state * @return 0 if no error, else return error code. */ -static int read_rle_sgi(uint8_t *out_buf, SgiState *s) +static int read_rle_sgi(uint8_t *last_line, SgiState *s) { uint8_t *dest_row; unsigned int len = s->height * s->depth * 4; GetByteContext g_table = s->g; - unsigned int y, z; unsigned int start_offset; int linesize, ret; @@ -141,11 +140,10 @@ static int read_rle_sgi(uint8_t *out_buf, SgiState *s) return AVERROR_INVALIDDATA; } - for (z = 0; z < s->depth; z++) { - dest_row = out_buf; - for (y = 0; y < s->height; y++) { + for (unsigned z = 0; z < s->depth; z++) { + dest_row = last_line; + for (int remaining_lines = s->height;;) { linesize = s->width * s->depth; - dest_row -= s->linesize; start_offset = bytestream2_get_be32(&g_table); bytestream2_seek(&s->g, start_offset, SEEK_SET); if (s->bytes_per_channel == 1) @@ -154,6 +152,9 @@ static int read_rle_sgi(uint8_t *out_buf, SgiState *s) ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth); if (ret != s->width) return AVERROR_INVALIDDATA; + if (--remaining_lines == 0) + break; + dest_row -= s->linesize; } } return 0; @@ -204,7 +205,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p, SgiState *s = avctx->priv_data; unsigned int dimension, rle; int ret = 0; - uint8_t *out_buf, *out_end; + uint8_t *out_buf, *last_line; bytestream2_init(&s->g, avpkt->data, avpkt->size); if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) { @@ -258,14 +259,14 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p, p->key_frame = 1; out_buf = p->data[0]; - out_end = out_buf + p->linesize[0] * s->height; + last_line = out_buf + p->linesize[0] * (s->height - 1); s->linesize = p->linesize[0]; /* Skip header. */ bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET); if (rle) { - ret = read_rle_sgi(out_end, s); + ret = read_rle_sgi(last_line, s); } else { ret = read_uncompressed_sgi(out_buf, s); } -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-09-30 17:05 UTC|newest] Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-09-30 17:02 [FFmpeg-devel] [PATCH 1/7] avcodec/sunrast: Use ptrdiff_t for stride Andreas Rheinhardt 2022-09-30 17:05 ` Andreas Rheinhardt [this message] 2022-09-30 17:05 ` [FFmpeg-devel] [PATCH 3/7] avcodec/sgidec: Avoid redundant private context Andreas Rheinhardt 2022-10-04 14:42 ` Tomas Härdin 2022-09-30 17:05 ` [FFmpeg-devel] [PATCH 4/7] avcodec/sgidec: Use planar pixel formats Andreas Rheinhardt 2022-09-30 17:05 ` [FFmpeg-devel] [PATCH 5/7] avcodec/c93: Fix segfault when using negative linesizes Andreas Rheinhardt 2022-09-30 17:05 ` [FFmpeg-devel] [PATCH 6/7] avcodec/escape124: Fix segfault with " Andreas Rheinhardt 2022-09-30 17:05 ` [FFmpeg-devel] [PATCH 7/7] avcodec/fraps: " Andreas Rheinhardt 2022-10-03 22:05 ` [FFmpeg-devel] [PATCH 1/7] avcodec/sunrast: Use ptrdiff_t for stride Andreas Rheinhardt 2022-10-04 0:03 ` James Almer 2022-10-04 0:25 ` Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=GV1P250MB0737584845B197233BFAF0208F569@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git