opj_mj2_extract: Check provided output prefix for length
authorKarol Babioch <kbabioch@suse.de>
Fri, 2 Mar 2018 13:40:58 +0000 (14:40 +0100)
committerEven Rouault <even.rouault@spatialys.com>
Sat, 22 Sep 2018 21:12:39 +0000 (23:12 +0200)
This uses snprintf() with correct buffer length instead of sprintf(), which
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

index a062e17d8acdc5d7ed51699905313760cb3c2ce4..fa67bf855989e9c2aa80d8bfe81a3dcdb5244d89 100644 (file)
@@ -140,10 +140,21 @@ 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");
+                free(frame_codestream);
+                return 1;
+            }
+        }
+
         outfile = fopen(outfilename, "wb");
         if (!outfile) {
             fprintf(stderr, "failed to open %s for writing\n", outfilename);
+            free(frame_codestream);
             return 1;
         }
         fwrite(frame_codestream, sample->sample_size - 8, 1, outfile);