#Building a toy shell in C

41 messages · Page 1 of 1 (latest)

unkempt cipher
#

Hey guys. I need help diagnosing what and where I'm going wrong with my shell implementation. When I enter things like ls ls env ls env I don't get secondary output, sometimes when. Ienter exit with debugging options, I get a SIGSEGV pointing to memory leaks. I don't know how to think about solving this.

vital wharfBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

unkempt cipher
knotty swan
#

for memory leaks you can always valgrind it

unkempt cipher
#

I've inspected valgrind output using -fsanitize=address -undefined, I truly don't understand how to interpret it beyond looking at the given file and trying to mess things up further.

knotty swan
#

what did valgrind say?

unkempt cipher
knotty swan
#

so you see how each leak has like a number of places where it's called?

#

#0 is like the internal stuff
#1 is where the leak originates from

unkempt cipher
#

So I care less about #0 asan stuff and more about #1 the callee and #2 the caller?

knotty swan
#

yes

unkempt cipher
#

Ok, I have three separate direct leaks.

#

So for the first one, all signs point to this call :path = calloc(1, strlen(ptr) + 1 + strlen(s[0]) + 2);

#

That's in findpath.c

#

This is called from forkcmd.c ``` path = findpath(s);
child = fork();
if (child == 0)
{
if (execve(path, s, NULL) == -1)
{
write(1, s[0], strlen(s[0]));
write(1, ": ", 2);
writes(CMDNF);
free(path);
return (-1);
}
}

wait(&child);
free(path);```
#

Maybe I'm allocating too much memory?

knotty swan
#

!cref calloc

vital wharfBOT
knotty swan
#

I didn't look into the semantic of your code yet but are you using calloc correctly?

#

it allocates num amount of size

unkempt cipher
#

Oh, maybe I'm not. Will switch those around and actually use sizeof(char) so I don't mess that up.

#
Direct leak of 869 byte(s) in 32 object(s) allocated from:
    #0 0xffff2331e650 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
    #1 0xaaaacf071d60 in findpath /home/muhammeda/src/alx/simple_path/findpath.c:15
    #2 0xaaaacf072040 in forkcmd /home/muhammeda/src/alx/simple_path/forkcmd.c:19```
#

That's when I use path = calloc(strlen(ptr) + 1 + strlen(s[0]) + 2, 1);

#

To repeat this issue, exit on a line by itself won't do: You'd have to do more than one call of ls

#

!cref realloc

vital wharfBOT
unkempt cipher
#

@knotty swan Do you think you have the time to go through the code with me?

#

Understand if you don't.

knotty swan
#

I'm on my computer now so I'll just test locally

unkempt cipher
vital wharfBOT
#

@unkempt cipher Has your question been resolved? If so, run !solved :)

unkempt cipher
#

Just noticed another bug when I ls ... Sigh.

unkempt cipher
#

👀

unkempt cipher
#

@knotty swan Still here?

knotty swan
#

yeah sorry I went to manage things and homework feelsbadman

#

maybe someone else can help on this thread

unkempt cipher
#

Damn, ok. Thanks.

vital wharfBOT
#

@unkempt cipher Has your question been resolved? If so, run !solved :)

unkempt cipher
#

!solved