1

I have this function in my program, in which the *sm pointer is showing error when reallocating. I have tried using malloc and free to manually resize the *sm but same error is showing in the free function.

char *numc_util_add_str(char **str, int s) {
    int size = 1;
    char *sm = (char *)malloc(size + 1);
    sm[0] = '0';
    sm[1] = '\0';
    while (s--) {
        int l = strlen(str[s]);
        int sz = l > size ? l : size;
        int t, r, car = 0;
        for (register int i = 0; i < sz; i++) {
            t = (i <= l ? str[s][i] - 48 : 0) + (i <= size ? sm[i] - 48 : 0) + car;
            r = t % 10;
            car = t / 10;
            sm[i] = 48 + r;
        }
        if (car) {
            size++;
            sm = (char *)realloc(sm, size + 1);
            sm[size - 1] = 48 + r;
        }
        sm[size + 1] = '\0';
    }
    return sm;
}

This is showing when debugging:

    sm = (char *)realloc(sm, size + 1);
// Exception has occurred.
// Trace/breakpoint trap
Barmar
  • 741,623
  • 53
  • 500
  • 612
Kumarjit
  • 13
  • 1
  • 3
  • 3
    Problem is actually a little later. `sm[size + 1] = '\0';` should be `sm[size] = '\0';`. – ikegami Feb 21 '20 at 01:33
  • Tip: The name `size` is usually used for the amount of allocated memory. `len` would be a more appropriate name for what your variable holds: the length of a string. – ikegami Feb 21 '20 at 01:35
  • 1
    Tip: `'0'` is more readable than `48`. – ikegami Feb 21 '20 at 01:35
  • 2
    Tip: Don't cast the result of `malloc`. The only thing that can achieve is errors. – ikegami Feb 21 '20 at 01:36
  • Could you explain what this is supposed to be doing? I can't figure out the logic of the `t=` line. – Barmar Feb 21 '20 at 01:39
  • 2
    Tip: Don't use `register`. The compiler can figure this out on its own. – Barmar Feb 21 '20 at 01:39
  • What does this have to do with WinAPI? It never calls any API functions. – Barmar Feb 21 '20 at 01:40
  • `i <= l` should probably be `i < l`, and the same with `i <= size`. – Barmar Feb 21 '20 at 01:41
  • 1
    [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/995714), [Is it useless to use the `register` keyword with modern compilers, when optimizing?](https://stackoverflow.com/q/43475229/995714), [Is the register keyword still used?](https://stackoverflow.com/q/10675072/995714), [Replacement for deprecated register keyword C++ 11](https://stackoverflow.com/q/20618008/995714) – phuclv Feb 21 '20 at 01:46

1 Answers1

1

You're assigning outside the bounds of sm.

This line:

int sz = l > size ? l : size;

sets sz to the larger of l and size. You then use sz as the bound of the for loop. In the loop you do:

sm[i] = 48 + r;

If l was larger than size, i will also become larger than size, but size is the maximum index of the sm array.

I don't understand what you're trying to do, so I'm not sure what the proper fix is. Maybe just do

sm = realloc(sm, sz);

before the for loop.

Barmar
  • 741,623
  • 53
  • 500
  • 612