Warning: the function returns the address of the local variable [enabled by default]

#include <string.h> #include<stdio.h> #include<stdlib.h> char *chktype(char *Buffer, int Size) { char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); sz=strlen(found)-strlen(found1); char type[sz]; strncpy(type, found1, sz-1); return(type); } void main(){ char *buffer = "HTTP/1.1 200 OK\r\nDate: Tue, 25 Jun 2013 16:27:16 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Encoding: gzip\r\nServer: gws\r\nX-XSS-Protection: 1; mode=block\r\nX-Frame-Options: SAMEORIGIN\r\nTransfer-Encoding: chunked\r\n\r\n"; char *extension = chktype (buffer, sizeof(buffer)); printf("%s\r\n", extension); } 

This gives:

 warning: function returns address of local variable [enabled by default] 

... and I cannot understand what is wrong here. When I run it, I expect the result to be text/html; charset=UTF-8 text/html; charset=UTF-8 , but its gibberish.

What does a warning mean?

+4
source share
4 answers

The chktype function allocates memory for an automatic variable on the stack, and then returns the address of this variable (i.e. a pointer to this variable).

The problem is that the variables allocated on the stack are automatically destroyed when they go out of scope (i.e., control passes outside the curly braces that define the function).

This means that you are essentially returning a pointer to an invalid memory location, which is bad news. In C-talk, this behavior is undefined. In practical terms, this leads to poor results or, possibly, even to failure.

 char *chktype(char *Buffer, int Size) { // This pointer variable is allocated on the stack, but that okay because // it a pointer to a string literal, which are always constant. // (Technically, you should add the "const" qualifier to the declaration.) const char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); sz=strlen(found)-strlen(found1); // Like all the above variables, the one is also allocated on the stack. // But it the source of your problem here, because it the one that // you are returning at the end of the function. // Problem is, it goes away at the end of the function! char type[sz]; strncpy(type, found1, sz-1); return(type); } 

The correct way to return char* from a function is to allocate new memory from the heap using the malloc (or calloc ) function. This means that the calling function will be responsible for freeing the memory used by the return value, otherwise your program will leak memory.
(Always put this requirement in the documentation for your function! Even if β€œdocumentation” means a comment above the declaration.)

For example, change your code like this:

 char *chktype(char *Buffer, int Size) { // This pointer variable is allocated on the stack, but that okay because // it a pointer to a string literal, which are always constant. // (Technically, you should add the "const" qualifier to the declaration.) const char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); sz=strlen(found)-strlen(found1); char *type = malloc(sz); // allocate memory from the heap strncpy(type, found1, sz-1); return(type); } 

Now, in the chktype calling function, you need to make sure that you call free when you are done with its return value:

 char *type = chktype(...); // do something free(type); 

Please note that reliable code should check the malloc result for the null pointer to make sure that it could not allocate the requested memory. If so, you need to handle the error somehow. For clarity, this is not shown above.

+13
source

Fast / hacker answer (?):

Do

 char type[sz]; 

in

 static char type[sz]; 

Long answer: The error is pretty clear, you are returning the address of a variable that will be destroyed as soon as the function returns . There are several ways around this.

One simple way is to make the type static , this will fix things by making the type variable the lifespan of the program , but this will mean that you cannot name it twice in a line , you need to print or copy the result before calling again.

Another way is to allocate memory for the char array in your function and hope you remember free it as soon as you finish with it. If you do not, you will have a memory leak . This does not suffer from the above disadvantage.

+6
source

When you declare type as char type[sz] , which gives you a local variable. The lifetime of this memory will end when the function returns. Instead, you need to dynamically allocate memory, for example using malloc .

 char *type = (char *) malloc (sz * sizeof (char)); 
+2
source

You are returning a type that points to an array that has been allocated on the stack and is invalid after the chktype() function chktype() .

You could highlight the result on the heap, for example:

 char * chktype(const char * buffer, int size) { char * strng = "Content-Type: "; char * found = strstr (buffer, strng); char * found1 = strstr(found, "\r\n"); size_t sz = strlen(found) - strlen(found1); char * type = calloc(sz, sizeof(*type)); if (type) { strncpy(type, found1, sz - 1); } return type; } 

However, you need free() to get the result after it is no longer needed.

0
source

Source: https://habr.com/ru/post/1488206/


All Articles