0

i searched alot with google and spend about 2 hours fixing this error. I get the following Error:
"Run-Time Check Failure #2 - Stack around the variable 'buffer' was corrupted."

I know it has to do something with my pointers, my sprintf_s functions or similar. This simple program is supposed to just replace ',' with '-'.

I hope someone can give me a hint to solve this. I know a lot of those questions exist but none of them helped me fix this error.

#include "stdafx.h"
#include <stdio.h>

int strReplace(char *buffer, const rsize_t sizeBuffer,
    char *source, const rsize_t sizeSource,
    const char *substring, const rsize_t sizeSubstring,
    const char *replace, const rsize_t sizeReplace)
{
    if (sizeBuffer < sizeSource || sizeBuffer < sizeReplace || sizeSource < sizeSubstring)
        return -1;

    char *p;

    p = strstr(source, substring);
    while (p != NULL)
    {
        strncpy_s(buffer, sizeBuffer, source, p - source);

        buffer[p - source] = '\0';

        sprintf_s(buffer + (p - source), sizeBuffer, "%s%s", replace, p + strlen(substring));

        strncpy_s(source, sizeSource, buffer, sizeBuffer);
        p = strstr(source, substring);
    }

    return 0;
}

int myFunc(char *source, const rsize_t sizeSource)
{
    char *substring = ",";
    char *replace = "-";
    char buffer[100];

    strReplace(&buffer, 100, source, sizeSource,
        substring, 2, replace, 2);

    return 0;
}

int main()
{
    char input[25] = "1,2,3,4,5,6,7,8,9,0";

    printf("input: %s\n", input);
    myFunc(&input, 25);

    _getch();

    return 0;
}
L. Messing
  • 70
  • 7
  • You should step through the `strReplace()` function in the debugger, and examine the variables after each step. – Barmar Dec 21 '17 at 00:05
  • @Barmar already did that following every variable. Had no luck with that. Maybe i'm doing something wrong there – L. Messing Dec 21 '17 at 00:07
  • I don't think it should cause a problem, but `sizeBuffer` should be `sizeBuffer-(p-source)` in the `sprintf_s()` call. – Barmar Dec 21 '17 at 00:10
  • Many implementations of string replacement can be found [here](https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c) – Barmar Dec 21 '17 at 00:12
  • @Barmar no that's okay. It is supposed to append the buffer. – L. Messing Dec 21 '17 at 00:14
  • It's not OK because you're starting from `buffer + (p-source)`, so you have to substract that from the available space. – Barmar Dec 21 '17 at 00:17
  • @Barmar Sorry i misread that comment. This solved it! If you provide a quick answer i will upvote it. Thanks! – L. Messing Dec 21 '17 at 00:23
  • When you have a function with 8 parameters and like 10 LOC, or when you have something like `const footype x` all over your function declarations, you should be concerned about the overall program design. Bad design gives bad code, and bad code gives bugs. – Lundin Dec 21 '17 at 09:47

1 Answers1

1

WHen you call sprintf_s(), you're starting from the middle of buffer. So you need to reduce the available space by that index. It should be:

    sprintf_s(buffer + (p - source), sizeBuffer - (p - source), "%s%s", replace, p + strlen(substring));
Barmar
  • 741,623
  • 53
  • 500
  • 612