Skip to content

Commit

Permalink
Add regression test for #21. (#22)
Browse files Browse the repository at this point in the history
* Add regression test for #21.

* Remove the calls to `calloc`, since a string will suffice.

* Fixed memory leak.
  • Loading branch information
allenh1 authored May 20, 2018
1 parent e43d2ee commit 44154dc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
24 changes: 9 additions & 15 deletions src/shell-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,31 @@ std::string replace(std::string str, const char * sb, const char * rep)
std::string env_expand(std::string s)
{
const char * str = s.c_str();
auto t = std::shared_ptr<char>(
reinterpret_cast<char *>(calloc(1024, sizeof(char))), free);
char * temp = t.get();
std::string temp;
int index;
for (index = 0; static_cast<size_t>(str - s.c_str()) < s.size(); ++str) {
// aight. Let's just do it.
if (*str == '$') {
// begin expansion
if (*(++str) != '{') {continue;}
// @todo maybe work without braces.
auto t2 = std::shared_ptr<char>(
reinterpret_cast<char *>(calloc(s.size(), sizeof(char))),
free);
// @todo maybe work without braces
std::string to_fetch;
++str;
char * temp2 = t2.get();
for (char * tmp = temp2; *str && *str != '}'; *(tmp++) = *(str++)) {
for (; *str && *str != '}';) {
to_fetch += *(str++);
}
if (*str == '}') {
++str;
const char * out = getenv(temp2);
const char * out = getenv(to_fetch.c_str());
if (nullptr == out) {
continue;
}
for (const char * t = out; *t; ++t) {
temp[index++] = *t;
}
temp += out;
}
} // if not a variable, don't expand.
temp[index++] = *str;
temp += *str;
}
return std::string(temp);
return temp;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/shell_bison.yy
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ command_word argument_list {

command_word:
WORD {
Command::currentSimpleCommand = std::make_unique<SimpleCommand>();
Command::currentSimpleCommand = std::make_unique<SimpleCommand>();
auto _ptr = std::shared_ptr<char>(strdup($1), free);
if(!strcmp($1, "fg")) fg=true;
else if(!strcmp($1, "bg")) bg=true;
if(!strcmp($1, "fg")) fg=true;
else if(!strcmp($1, "bg")) bg=true;
else if(!strcmp($1, "pushd")) pushd=true;
else Command::currentSimpleCommand->insertArgument(_ptr);
else Command::currentSimpleCommand->insertArgument(_ptr);
delete[] $1;
}
;

Expand Down
28 changes: 28 additions & 0 deletions test/issue-21-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# Test for GitHub issue #21.
# See: https://github.com/allenh1/YaSh/issues/21

# this is the command that broke it.
cd_cmd="cd .././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././/./././././././././././././././././././././././././././././././././././././././././././././.././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././/./././././././././././././././././././././././././././././././././././././././././././././.././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././/./././././././././././././././././././././././././././././././././././././././././././././.././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././/./././././././././././././././././././././././././././././././././././././././././././././"

echo "cd ../../../../" > simple_cd
echo "pwd" >> simple_cd

echo ${cd_cmd} > shane_cd
echo "pwd" >> shane_cd

../src/yash < simple_cd &>> simple_out
../src/yash < shane_cd &>> shane_out

diff simple_out shane_out

if [[ $? -ne 0 ]]; then
echo "Issue #21 regression test failed"
rm -f simple_out shane_out simple_cd shane_cd
exit 1
fi

echo "Issue 21 test OK"
rm -f simple_out shane_out simple_cd shane_cd
exit 0
13 changes: 10 additions & 3 deletions test/testall
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ grade48max=5
echo "--- issue 48 regression -- ($grade48max pts)"
./issue-48-test && grade48=`expr $grade48 "+" 5`

grade21=0
grade21max=5
echo "--- issue 21 regression -- ($grade21max pts)"
./issue-21-test && grade21=`expr $grade21 "+" 5`

# grade25=0
# grade25max=5
# echo "--- Multi-Arg cd -- ($grade-25max pts)"
Expand All @@ -125,15 +130,16 @@ echo "--- issue 48 regression -- ($grade48max pts)"
total=`expr $grade1 + $grade2 + $grade3 + $grade4 + $grade5 + \
$grade6 + $grade7 + $grade8 + $grade9 + $grade10 + \
$grade11 + $grade12 + $grade13 + $grade53 + $grade58 + \
$grade48`
$grade48 + $grade21`

totalmax=`expr $grade1max + $grade2max + $grade3max + $grade4max \
+ $grade5max + $grade6max + $grade7max + $grade8max \
+ $grade9max + $grade10max + $grade11max + $grade12max \
+ $grade13max + $grade53max + $grade58max + $grade48max`
+ $grade13max + $grade53max + $grade58max + $grade48max \
+ $grade21max`

echo "-------------------------------------------------"
echo "User: $1 "
echo "YaSh Tests"
echo "-------------------------------------------------"
printf "IO Redirection: %-3d%s%-3d\n" $grade1 " of " $grade1max
printf "Pipes: %-3d%s%-3d\n" $grade2 " of " $grade2max
Expand All @@ -151,6 +157,7 @@ printf "issue 33: %-3d%s%-3d\n" $grade13 " of " $grade13max
printf "issue 53: %-3d%s%-3d\n" $grade53 " of " $grade53max
printf "issue 58: %-3d%s%-3d\n" $grade58 " of " $grade58max
printf "issue 48: %-3d%s%-3d\n" $grade48 " of " $grade48max
printf "issue 21: %-3d%s%-3d\n" $grade21 " of " $grade21max
echo "--------------------------------------------------"
printf "Total: %-3d%s%-3d\n" $total " of " $totalmax

Expand Down

0 comments on commit 44154dc

Please sign in to comment.