Search code examples
cbuffer-overflow

the error not resolved: heap-buffer-overflow on Leetcode


My code is below:

int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int count;
    int* out = malloc(500);
    int i, j;

    for(i = 0; i < numsSize; ++i){
        count = 0;
        for(j = 0; j < numsSize; ++j){
            if(nums[i] > nums[j]){
                count++;
            }
        }
        *(out + i) = count;
    } 

    return out;
}

and the error messages are below

enter image description here

Actually, this heap-buffer-overflow kept coming up when I've solved the problems on Leetcode. even if it works well in Terminal on Mac.

will appreciate any comments or helps on it!


Solution

  • At first, you don't check malloc function.

    Secondly, the argument returnSize is not used in this function.

    Your function will be failed if numsSize > (500/ sizeof(int))

    May be the code below can help you

    int* smallerNumbersThanCurrent(int* nums, int numsSize){
       int count;
       int* out = malloc(numsSize * sizeof(int));
       if (out) {
           fprintf(stderr, "%s: malloc failed\n", __func__);
           exit(EXIT_FAILURE);
       }
    
       for(int i = 0; i < numsSize; ++i){
           count = 0;
           for(int j = 0; j < numsSize; ++j){
                if(nums[i] > nums[j]){
                   count++;
                }
           }
          *(out + i) = count;
      } 
    
       return out;
    }