Skip to content

fixed: Buffer Overflow Vulnerability in countFields Function#245

Open
ayushman1210 wants to merge 8 commits intoPecanProject:masterfrom
ayushman1210:buffer_overflow
Open

fixed: Buffer Overflow Vulnerability in countFields Function#245
ayushman1210 wants to merge 8 commits intoPecanProject:masterfrom
ayushman1210:buffer_overflow

Conversation

@ayushman1210
Copy link
Contributor

Buffer Overflow Vulnerability in countFields() Function

Location: src/common/util.c, lines 51-62
Description:
The countFields() function has a fixed-size buffer of 256 bytes but uses strcpy() without bounds checking

int countFields(const char *line, const char *sep) {
  // strtok modifies string, so we need a copy
  char lineCopy[256];
  strcpy(lineCopy, line);  // UNSAFE - No bounds checking!
  // ... rest of function
}

If the input line parameter exceeds 255 characters (plus null terminator), strcpy() will write beyond the buffer boundary, causing a buffer overflow. This can lead to:

  • Stack corruption
  • Segmentation faults
  • Potential security exploits
  • Unpredictable program behavior

This function is called from:

  • readClimData() in sipnet.c (line 164)
  • checkParamFormat() in modelParams.c (line 129)
    Both functions process user-supplied input files, making this a exploitable vulnerability.

changes made
Replace strcpy() with strncpy() or use dynamic allocation

@ayushman1210
Copy link
Contributor Author

Hi @dlebauer @Alomir just wanted to check in and see if you had any thoughts on this PR when you get a chance.
Happy to make changes or answer questions.
Thanks!!

Copy link
Collaborator

@Alomir Alomir left a comment

Choose a reason for hiding this comment

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

Thanks for the work on this!

@ayushman1210 ayushman1210 requested a review from Alomir February 3, 2026 18:07
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.

2 participants