Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_include.h: Includes files in commented #includes, leading to recursive inclusion and stack-overflow. #1679

Open
OetkenPurveyorOfCode opened this issue Aug 30, 2024 · 3 comments

Comments

@OetkenPurveyorOfCode
Copy link

OetkenPurveyorOfCode commented Aug 30, 2024

Describe the bug
stb_include.h does not distinguish between in an #include directive and a commented #include directive.
Let's say you have a header file with some documentation how to use it:

/*
Here is how you use my library foo.h:
#include "foo.h"

bla bla bla
*/

This recursively includes itself when processed by stb_include.h.

To Reproduce
Steps to reproduce the behavior:

  1. Create a file test.h:
/*
#include "test.h"
*/
  1. Write an include preprocessor using stb_include.h
    include_preprocessor.c
#define STB_INCLUDE_IMPLEMENTATION
#include "stb_include.h"
#include <stdio.h>

int main(void) {
    char error[257] = {0};
    size_t len1 = 0;
    char* instr = stb_include_load_file("test.h", &len1);
    char* preprocessed = stb_include_string(instr, "", ".", "", error);
    //char* preprocessed = stb_include_file("pix.c", "", ".", error);
    if (!preprocessed) {
        printf("error: %s\n", error);
        puts("Error preprocessing");
        return 0;
    }
    
    FILE* outfile = fopen("pix_single.c", "wb+"); // Text mode possible as well
    if (!outfile) {
        goto outfile_error;
    }
    size_t len = strlen(preprocessed);
    if (fwrite(preprocessed, 1, len, outfile) != len) {
        goto outfile_error;
    }
    if (fclose(outfile) == EOF) {
        goto outfile_error;
    }
    goto success;
    outfile_error:
        puts("Writing to outfile failed");
        return -1;
    success:
        puts("Succeeded");
        return 0;
}
  1. Compile:
clang -fsanitize=address include_processor.c -o include_processor.exe
  1. Run
=================================================================
==9728==ERROR: AddressSanitizer: stack-overflow on address 0x7ffcc7aca265 (pc 0x7ffcc7aca265 bp 0x00f8594041c0 sp 0x00f859404000 T0)
    <empty stack>

SUMMARY: AddressSanitizer: stack-overflow
==9728==ABORTING

Expected behavior
Ignore commented #includes.

@OetkenPurveyorOfCode OetkenPurveyorOfCode changed the title stb_include.h: Includes files in comment, leading to recursive inclusion and stack-overflow. stb_include.h: Includes files in commented #includes, leading to recursive inclusion and stack-overflow. Aug 30, 2024
@SX1109
Copy link

SX1109 commented Sep 25, 2024

Improved Bug Report

Describe the bug
The file stb_include.h fails to differentiate between an #include directive within active code and one that's inside a comment. As a result, it processes even commented #include directives, which can lead to recursive inclusion issues.
For example, consider a header file with some documentation showing how to use it:

/*
Here is how you use my library foo.h:
#include "foo.h"
bla bla bla
*/

When processed by stb_include.h, it incorrectly includes itself recursively, leading to stack overflow or other issues.

Steps to reproduce

  1. Create a file test.h with the following content:

    /*
    #include "test.h"
    */
  2. Write an include preprocessor using stb_include.h:

    #define STB_INCLUDE_IMPLEMENTATION
    #include "stb_include.h"
    #include <stdio.h>
    
    int main(void) {
        char error[257] = {0};
        size_t len1 = 0;
        char* instr = stb_include_load_file("test.h", &len1);
        char* preprocessed = stb_include_string(instr, "", ".", "", error);
        if (!preprocessed) {
            printf("error: %s\n", error);
            puts("Error preprocessing");
            return 0;
        }
        
        FILE* outfile = fopen("output.c", "wb+");
        if (!outfile) {
            puts("Failed to open output file");
            return -1;
        }
        
        size_t len = strlen(preprocessed);
        if (fwrite(preprocessed, 1, len, outfile) != len) {
            puts("Failed to write to output file");
            return -1;
        }
        
        fclose(outfile);
        puts("Succeeded");
        return 0;
    }
  3. Compile the code:

    clang -fsanitize=address include_preprocessor.c -o include_preprocessor
  4. Run the executable:

    ./include_preprocessor

Expected behavior
The stb_include.h preprocessor should ignore #include directives that are inside comments. It should only process active, uncommented directives. This would prevent recursive inclusion and stack overflow issues.

Actual behavior
The program throws a stack overflow error due to the recursive inclusion caused by not distinguishing between commented and uncommented #include directives.

==9728==ERROR: AddressSanitizer: stack-overflow on address 0x7ffcc7aca265
<empty stack>

SUMMARY: AddressSanitizer: stack-overflow
==9728==ABORTING

Suggestions for Improvement

  1. Detecting Comments:
    The issue can be resolved by adding logic in stb_include.h to correctly handle comments. This could be done by detecting // and /* ... */ comment patterns before processing any #include directives.

  2. Preprocessing Stage:
    Introduce a preliminary pass over the file that strips out or skips over commented sections. This ensures that the include processor only deals with uncommented code.

  3. Recursive Limitations:
    Implement a recursion limit or stack depth check in stb_include.h to prevent infinite inclusion loops, even if the file contains uncommented recursive #include directives.

  4. Edge Case Handling:
    Ensure that edge cases like nested comments or mixed comment types (/*...*/ and //) are handled correctly to avoid false positives or missed cases.

These changes should prevent recursive includes from commented directives, thereby avoiding stack overflow issues in complex projects.

@OetkenPurveyorOfCode
Copy link
Author

@SX1109 You are not helping anyone by letting ChatGPT rephrase pre existing bugreports.

@OetkenPurveyorOfCode
Copy link
Author

Oh. I had not seen that issue #954 describes the bug already: #954

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants