Skip to content

Fix null pointer dereference in ngram_model_read#438

Merged
dhdaines merged 2 commits intomainfrom
fix-ngram-null-check
Oct 23, 2025
Merged

Fix null pointer dereference in ngram_model_read#438
dhdaines merged 2 commits intomainfrom
fix-ngram-null-check

Conversation

@lenzo-ka
Copy link
Contributor

Description

Fixes a null pointer dereference when reading malformed or empty ARPA language model files.

The NGRAM_ARPA case in ngram_model_read did not check if the model was NULL before proceeding, unlike other file type cases. This caused a crash in ngram_model_apply_weights when processing invalid files.

Changes:

  • Added null check after ngram_model_trie_read_arpa call
  • Returns NULL immediately if model read fails, consistent with other cases

Verification steps

  • Compiled successfully with no errors
  • Prevents NULL pointer dereference crash reported in the issue
  • Matches existing error handling pattern used in NGRAM_BIN case

Fixes #432

Replace unsafe %s format specifier with %c%c%c%c to prevent
reading past the 4-byte non-null-terminated id buffer.

Fixes #431
Add null check for NGRAM_ARPA case to prevent crash when
reading malformed or empty language model files.

Fixes #432
@lenzo-ka lenzo-ka requested a review from dhdaines October 23, 2025 18:21
Copy link
Contributor

@dhdaines dhdaines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh! Yes another one of these error string formatting issues. The NULL pointer check is totally sensible as well.

@dhdaines dhdaines merged commit 71b75d4 into main Oct 23, 2025
21 checks passed
@dhdaines dhdaines deleted the fix-ngram-null-check branch October 23, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Missing null check in ngram_model_read for NGRAM_ARPA

2 participants