opj_mj2_extract: Check provided output prefix for length

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.
This commit is contained in:
Karol Babioch 2018-03-02 14:40:58 +01:00
parent 564fbfb678
commit b1f02d4d6f
1 changed files with 6 additions and 1 deletions

View File

@ -140,7 +140,12 @@ int main(int argc, char *argv[])
fread(frame_codestream, sample->sample_size - 8, 1, fread(frame_codestream, sample->sample_size - 8, 1,
file); /* Assuming that jp and ftyp markers size do*/ 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"); outfile = fopen(outfilename, "wb");
if (!outfile) { if (!outfile) {
fprintf(stderr, "failed to open %s for writing\n", outfilename); fprintf(stderr, "failed to open %s for writing\n", outfilename);