Skip to content

Fix buffer overflow in fscan_pnm_token (pnm2png)#1

Closed
sanjay20m wants to merge 1 commit intolibpng16from
sanjay20m-patch-100
Closed

Fix buffer overflow in fscan_pnm_token (pnm2png)#1
sanjay20m wants to merge 1 commit intolibpng16from
sanjay20m-patch-100

Conversation

@sanjay20m
Copy link
Owner

Summary

This PR fixes a buffer overflow vulnerability in the fscan_pnm_token function in contrib/pngminus/pnm2png.c, which is used by the pnm2png conversion tool.

Vulnerability

The original implementation lacked bounds checking when reading tokens from the PNM header. If a header line (e.g., width or height) was excessively long, the function could write past the end of the token buffer, potentially causing a crash or denial of service.

Fix

The patch adds a check to ensure that the buffer is not overrun:

if (i >= token_buf_size - 1)
    break;
token_buf[i++] = (char) ret;

Impact : 	
•	Prevents malformed PNM files from crashing pnm2pngKeeps the core libpng unaffectedNo regressions in functionality or parsing logic

The fscan_pnm_token function in contrib/pngminus/pnm2png.c did not
properly check for buffer boundaries while reading numeric tokens from
PNM headers. This could lead to a buffer overflow when parsing very
long lines, potentially causing denial-of-service behavior.

This patch adds a length check to ensure that the token_buf array is
not written beyond its allocated size. The buffer is now safely
null-terminated and the maximum allowed characters are respected.

This affects the pnm2png conversion utility only, and not the core
libpng library.
@sanjay20m sanjay20m closed this Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant