From 6d8c0c06ee32dc03ba80acd48334e98728e56cf5 Mon Sep 17 00:00:00 2001
From: Karol Babioch <kbabioch@suse.de>
Date: Fri, 2 Mar 2018 14:40:58 +0100
Subject: [PATCH] opj_mj2_extract: Check provided output prefix for length

This uses snprintf() with correct buffer length instead of sprintf(). This
prevents a buffer overflow when providing a long output prefix. Furthermore
the program exits with an error when the provided output prefix is too long.

Fixes #1088.
---
 src/bin/mj2/opj_mj2_extract.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/bin/mj2/opj_mj2_extract.c b/src/bin/mj2/opj_mj2_extract.c
index a062e17d8..244110523 100644
--- a/src/bin/mj2/opj_mj2_extract.c
+++ b/src/bin/mj2/opj_mj2_extract.c
@@ -140,7 +140,12 @@ int main(int argc, char *argv[])
         fread(frame_codestream, sample->sample_size - 8, 1,
               file); /* Assuming that jp and ftyp markers size do*/
 
-        sprintf(outfilename, "%s_%05d.j2k", argv[2], snum);
+        int num = snprintf(outfilename, sizeof(outfilename), "%s_%05d.j2k", argv[2], snum);
+        if (num >= sizeof(outfilename)) {
+            fprintf(stderr, "maximum length of output prefix exceeded\n");
+            return 1;
+        }
+
         outfile = fopen(outfilename, "wb");
         if (!outfile) {
             fprintf(stderr, "failed to open %s for writing\n", outfilename);
