akorentlab / leptonica

Automatically exported from code.google.com/p/leptonica
0 stars 0 forks source link

Image corruption when reading valid PNM files #101

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run attached program that includes test cases for the problem

What is the expected output? What do you see instead?
All 5 test cases should output SUCCESS but three output FAILED

What version of the product are you using? On what operating system?
Leptonica 1.71, CentOS 6.5

Please provide any additional information below.
Bug appears to be in pnmio.c at function freadHeaderPnm(). Function uses fscanf 
to read decimal values separated by whitespaces. However in case there are 
extra whitespace characters after the last number read the fscanf will skip 
them all. Since only one white space is part of the header the rest skipped 
bytes were part of image data and thus read image is invalid.

The attached file contains sample code that can be easily used to verify the 
problem.

Original issue reported on code.google.com by vesa.sal...@gmail.com on 1 Aug 2014 at 12:51

Attachments:

GoogleCodeExporter commented 9 years ago
Vesa, I'm very sorry to have ignored your valid bug report.  I was on vacation 
and didn't see it.  You are exactly right; fscanf swallowed the 32, 10 and 9 
because they're all white space.

The fix is to use fgets into a buffer, and then sscanf, for the last number 
read from the header.  I used a similar remedy to read data in pixcomp.c.

Version 1.72 is coming out within a week (to leptonica.org, not here -- uploads 
are no longer allowed on code.google.com), and you can get it from there.  Or I 
can send the patched file directly to you: you are vesa.salento@aalto.fi, 
correct?

Again, my apologies, and thank you for the contribution.

  -- Dan

Original comment by dan.bloo...@gmail.com on 10 Apr 2015 at 6:45

GoogleCodeExporter commented 9 years ago
One additional thing.  The second test now fails, because the maxval line does 
not end with a newline.  Looking at the spec, a newline is NOT required (yet 
another idiocy of the format), so a lower-level approach is required.

Original comment by dan.bloo...@gmail.com on 10 Apr 2015 at 8:55

GoogleCodeExporter commented 9 years ago
The fix is to use fgetc repeatedly, checking each time.  Fixed in 1.72.

Original comment by dan.bloo...@gmail.com on 10 Apr 2015 at 10:43