2

Warning C6386, as described by Microsoft, "...indicates that the writable extent of the specified buffer might be smaller than the index used to write to it. This defect can cause buffer overrun". This is the kind of programming error made by beginners (no hate, we all started somewhere). I'm looking for help from experienced (C) programmers. VS seems to think my code is capable of committing this error.

Here is the simplest case that produces this error, along with the error message:

void func(const unsigned int n)
{
    unsigned int i;
    float *const arr = malloc( sizeof(*arr) * n );
    if (!arr) return;

    for (i = 0; i < n; i++)
    {
        arr[i] = 3.14f;     // This line produces warning C6386
    }
}

// warning C6386: Buffer overrun while writing to 'arr':  the writable size is 'sizeof((*arr))*n' bytes, but '8' bytes might be written.

Is this a bug within VS Code Analysis? Thanks.

yuno
  • 31
  • 2
  • Very similar to https://stackoverflow.com/questions/41943803/visual-studio-2015-code-analysis-c6386-warns-of-buffer-overrun - the static analyzer probably failed to decode what `sizeof((*arr))*n` could be and if it compares favorably to 8. A false positive rather than a bug. – teapot418 Apr 12 '23 at 19:19
  • @teapot418 While I agree it's similar, I believe this is well defined in comparison. `n` and `arr` are constant, and `arr` cannot be NULL by the time the line in question is reached. `i` is necessarily non-negative. `3.14` is necessarily a float. What could cause a static analyzer to overlook this? – yuno Apr 12 '23 at 20:44

0 Answers0