Skip to content

Commit

Permalink
[mycpp/runtime] Fix leak when using getline()
Browse files Browse the repository at this point in the history
Python-2.7.13 configure now runs with no crashes or leaks!  Woohoo !!!
  • Loading branch information
Andy C committed Dec 15, 2022
1 parent 7f57927 commit afc169f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 11 additions & 2 deletions benchmarks/gc.sh
Expand Up @@ -289,13 +289,22 @@ gc-run-oil() {

local i=0
for script in */*.sh; do
echo "=== $script"
case $script in
(build/clean.sh|build/common.sh|build/dev.sh)
# Top level does something!
echo "=== SKIP $script"
continue
;;
esac

echo
echo "=== ($i) $script"

# Just run the top level, which (hopefully) does nothing
OIL_GC_STATS=1 OIL_GC_THRESHOLD=1000 OIL_GC_ON_EXIT=1 $bin $script

i=$((i + 1))
if test $i -gt 3; then
if test $i -gt 60; then
break
fi
done
Expand Down
9 changes: 6 additions & 3 deletions mycpp/gc_mylib.cc
Expand Up @@ -18,7 +18,7 @@ MutableStr* NewMutableStr(int cap) {
return reinterpret_cast<MutableStr*>(NewStr(cap));
}

// NOTE: split_once() was in gc_mylib, and is likely not leaky
// TODO: remove unnecessary rooting
Tuple2<Str*, Str*> split_once(Str* s, Str* delim) {
StackRoots _roots({&s, &delim});

Expand Down Expand Up @@ -72,6 +72,8 @@ Str* CFileLineReader::readline() {
errno = 0;
ssize_t len = getline(&line, &allocated_size, f_);
if (len < 0) {
// man page says the buffer should be freed even if getline fails
free(line);
if (errno != 0) { // Unexpected error
log("getline() error: %s", strerror(errno));
throw Alloc<IOError>(errno);
Expand All @@ -80,9 +82,10 @@ Str* CFileLineReader::readline() {
return kEmptyString;
}

// TODO: Fix the leak here.
// Note: getline() NUL terminates the buffer
return ::StrFromC(line, len);
Str* result = ::StrFromC(line, len);
free(line);
return result;
}

// Problem: most Str methods like index() and slice() COPY so they have a
Expand Down

0 comments on commit afc169f

Please sign in to comment.