Skip to content

Confusing example code for demonstrating off-by-one errors. #173

@mayank-ramnani

Description

@mayank-ramnani

int len = 0, total = 0;
while(1){
fgets(buff1, MAX_SIZE, stdin);
int len = strnlen(buff1, MAX_SIZE);
total += len;
if(total < MAX_SIZE) strncat(buff2, buff1, len);
else break;
}

Both the snippets (vulnerable and non-vulnerable one) have various flaws:

  1. Variable shadowing (int len is declared twice)
  2. Business logic faults: The last string that is read in is completely discarded. Characters that can fit in the buffer should be appended, and the rest should be discarded instead of discarding the whole thing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions