-1

I am trying to write to a C string character-by-character but Visual Studio indicates that the pointer in question is NULL. Even though there are two functions that attempt this, only in the first one does this issue occur.

These are the two functions:

char* revstr(char* string)
{
    int len = strlen(string);
    char* retstr = (char*)malloc(30);
   // printf("%d\n", sizeof(retstr));
    int i=0, j=0;
    for (i = len, j = 0; i >=0; i--, j++)
    {
        retstr[j]=string[i]; // this generates C6011: dereferencing NULL pointer 'retstr'
                                
    }
    printf("\n");
    retstr[j] = '\0'; // also generates C6011
    printf("%s",etstr);
    printf("\n");
   
    
    return retstr;
}

char * get_name(char* string)
{
    char* retstr = (char*)malloc(30);
    int i = 0,j=0;
    for (i = strlen(string) - 1; i > 0; i--)
        if (string[i] == '.')
            break;
    while (string[i] != '/')
    {
       // printf("%c", string[i]);
     retstr[j] = string[i];
        i--; j++;

    }
    retstr[j] = '\0';

    return retstr;
}
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • 1
    What is the point in listing `get_name` function source in the question? It isn't used anywhere. – arrowd Sep 26 '21 at 08:52
  • There's [some discussion about casting the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) but the consensus is: *Don't* do that cast. It can hide errors that are hard to diagnose. – Some programmer dude Sep 26 '21 at 08:53
  • 1
    `for (i = len - 1, j = 0; i >=0; i--, j++)` - note the `-1`. Btw, why are you allocating 30 `char`s? You should be allocating `len + 1` `char`s – Ted Lyngmo Sep 26 '21 at 08:53
  • And if you know the exact length of the string you need to create (as it's based on `string`) then why hard-code to always allocate `30` bytes? What happens if `string` is shorter than that? What happens if it's *longer*? – Some programmer dude Sep 26 '21 at 08:55
  • Separately from the issue, for experienced C developers, this is an unusual way to reverse a string. They would more likely not allocate memory and just reverse the characters in the string that was passed as function argument. For anyone interested in receiving a new string, they could just first call `strdup` and have that reversed. – Cheatah Sep 26 '21 at 09:01
  • 3
    Questions like this should never come without a [mcve] and the full error message. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Sep 26 '21 at 09:05

1 Answers1

3

C6011 is a warning, not an error. It says that you may dereference a NULL pointer.

What the compiler means is that malloc may fail, in theory with any amount, and that you are not checking that. If it were to return NULL you program will show undefined behavior.

The easiest way to avoid that, if you don't care about out of memory situations, is just add:

char* retstr = (char*)malloc(30);
if (!retstr)
    abort();

The proper way would be to return an error and propagate it upwards and handle it gracefully in the user interface layer, but that is not always so easy.

rodrigo
  • 94,151
  • 12
  • 143
  • 190