PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] How can i solve this warning C6386

 April 27, 2022     c, compiler-warnings, visual-studio, warnings     No comments   

Issue

How can i solve the C6386 warning that occurs at the line inside for loop?

reverseString[i] = str[size - i - 1]; Warning occurs on this line.

The exact error is: "C6386: Buffer overrun while writing to 'reverseString': the writable size is '((size+1))*sizeof(char)' bytes, but '2' bytes might be written."

Function can be found below:

char* reverseString(char* str) {
    if (str == NULL) {
        printf("input error\n");
        return NULL;
    }

    int size = strlen(str);
    char* reverseString = malloc((size + 1) * sizeof(char));
    if (reverseString != NULL) {
        for (int i = 0; i < size; i++) {
            reverseString[i] = str[size - i - 1];
        }
        reverseString[size] = '\0';
        
        return reverseString;
    }
    else {
        printf("error while allocating memory\n");
        return NULL;
    }
}

Solution

Found the reason why this warning occurs from here.

For this particular example, since 'size + 1' never becomes 0, this warning can be ignored. And if i check that 'size + 1' is greater than 0 before calling malloc the warning goes away.



Answered By - SaffronBrick
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing