Login Register






Simple Shell Program filter_list
Author
Message
RE: Simple Shell Program #13
I know the heap is more versatile and larger than the stack, some places in your code it makes sense but for example:
Code:
int main(){ ... char *login; //max login length is 32 bytes, 33 for good luck login = malloc(33 *sizeof(char)); memset(login, 0, 33); //get the login retLogin(&login); do{ printf("%c%s%c%c ", '$', login, '_', '>'); ... }while(status); ... free(login); return EXIT_SUCCESS; }

With login it doesn't really make sense. You allocate 33 bytes, zero-initialize that, then pass it to a function and printf() the result in the loop.

Your retLogin function (tmp) is a pointer to your pointer:
Code:
//getlogin is deprecated on ArchLinux (& newer distro's in general) //works fine on the servers, for the purposes of this proj int retLogin(char **tmp){ char *getlog = *tmp; getlog = getlogin(); *tmp = getlog; if(!getlog){ die("getlogin() error"); free(getlog); return -1; }else return 0; }

Code:
char *getlog = *tmp; getlog = getlogin();

The first assignment of getlog to the (char *) dereferenced from tmp is overwritten in the next line where it's assigned the return of getlogin(). Then the (char *) dereferenced from tmp which is essentially the pointer which login points to is assigned to the pointer returned by getlogin() again.

Code:
*tmp = getlog;

HOWEVER - getlogin() returns a pointer to a STATIC buffer, so now that your login pointer doesn't any longer point to the memory you've allocated with malloc(), you attempt to free this static buffer which isn't even on the heap, and you leak memory from the call to malloc().

Read the docs for getlogin(): https://linux.die.net/man/3/getlogin
Quote:The string is statically allocated and might be overwritten on subsequent calls to this function or to cuserid().

Essentially, this is what the function does internally:
Code:
function getlogin() { static char buf[BUF_SIZE]; ... get login ... return buf; }

^ This is more likely where the memory leak comes from. The problem is that you allocate memory on the heap, pass a pointer to the pointer (that holds the address of the first byte of that allocated memory on the heap) to a function that modifies the address to some memory on the stack, then you try to free it as a subsequent call to the malloc() which not only doesn't free the memory on the heap but introduces undefined behavior!

Summary:
- You allocate memory on the heap for your login pointer
- The function you pass a pointer to this pointer to does some redundant pointer assignments before assigning your pointer to a location on the stack
- Return to caller
- You attempt to free the memory from the stack (not what's on the heap from the initial call to malloc())
- ** Memory leak ** + ** Undefined behavior **
(This post was last modified: 11-30-2016, 06:48 PM by bitm0de.)
- mostly braindead monkeys on this forum.

Reply





Messages In This Thread
Simple Shell Program - by insidious - 10-16-2016, 04:22 AM
RE: Simple Shell Program - by phyrrus9 - 10-16-2016, 07:42 AM
RE: Simple Shell Program - by Inori - 10-17-2016, 11:58 PM
RE: Simple Shell Program - by phyrrus9 - 10-18-2016, 12:24 AM
RE: Simple Shell Program - by Inori - 10-18-2016, 12:25 AM
RE: Simple Shell Program - by phyrrus9 - 10-18-2016, 12:36 AM
RE: Simple Shell Program - by insidious - 10-18-2016, 05:44 AM
RE: Simple Shell Program - by phyrrus9 - 10-18-2016, 05:46 AM
RE: Simple Shell Program - by insidious - 10-18-2016, 05:51 AM
RE: Simple Shell Program - by phyrrus9 - 10-18-2016, 05:54 AM
RE: Simple Shell Program - by bitm0de - 11-30-2016, 08:02 AM
RE: Simple Shell Program - by insidious - 11-30-2016, 05:14 PM
RE: Simple Shell Program - by bitm0de - 11-30-2016, 06:13 PM