memory leaks

This commit is contained in:
Vitor Santos Costa 2016-05-30 11:25:28 +01:00
parent 4ac6d11dca
commit 71c49f5063
5 changed files with 75 additions and 52 deletions

View File

@ -261,7 +261,7 @@ void Yap_DefaultStreamOps(StreamDesc *st) {
Yap_ConsoleOps(st);
}
#ifndef _WIN32
else if (st->file != NULL && 0 && !(st->status & InMemory_Stream_f)) {
else if (st->file != NULL && 0 && !(st->status & InMemory_Stream_f)) {
if (st->encoding == Yap_SystemEncoding()) {
st->stream_wgetc = get_wchar_from_file;
} else
@ -1205,7 +1205,7 @@ do_open(Term file_name, Term t2,
if (args[OPEN_ALIAS].used) {
Atom al = AtomOfTerm(args[OPEN_ALIAS].tvalue);
if (!Yap_AddAlias(al, sno)) {
free(args);
free(args);
return false;
}
}
@ -1276,9 +1276,8 @@ do_open(Term file_name, Term t2,
if ((fd = fopen(fname, io_mode)) == NULL ||
(!(flags & Binary_Stream_f) && binary_file(fname))) {
strncpy(LOCAL_FileNameBuf, fname, MAXPATHLEN);
if (fname != fbuf && fname != LOCAL_FileNameBuf &&
fname != LOCAL_FileNameBuf2)
free((void *)fname);
if (fname != fbuf)
freeBuffer((void *)fname);
fname = LOCAL_FileNameBuf;
UNLOCK(st->streamlock);
if (errno == ENOENT) {
@ -1314,6 +1313,8 @@ do_open(Term file_name, Term t2,
Yap_DefaultStreamOps(st);
if (script)
open_header(sno, open_mode);
if (fname != fbuf)
freeBuffer(fname);
free(args);
UNLOCK(st->streamlock);
@ -1437,7 +1438,8 @@ static Int p_file_expansion(USES_REGS1) { /* '$file_expansion'(+File,-Name) */
PlIOError(INSTANTIATION_ERROR, file_name, "absolute_file_name/3");
return (FALSE);
}
if (!Yap_findFile(RepAtom(AtomOfTerm(file_name))->StrOfAE, NULL, NULL, LOCAL_FileNameBuf, true, YAP_ANY_FILE, true, false))
if (!Yap_findFile(RepAtom(AtomOfTerm(file_name))->StrOfAE, NULL, NULL,
LOCAL_FileNameBuf, true, YAP_ANY_FILE, true, false))
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name,
"absolute_file_name/3"));
return (Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf))));
@ -1582,18 +1584,16 @@ int Yap_CheckTextStream__(const char *file, const char *f, int line, Term arg,
}
int Yap_CheckBinaryStream__(const char *file, const char *f, int line, Term arg,
int kind, const char *msg) {
int kind, const char *msg) {
int sno;
if ((sno = CheckStream__(file, f, line, arg, kind, msg)) < 0)
return -1;
if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (kind == Input_Stream_f)
PlIOError__(file, f, line, PERMISSION_ERROR_INPUT_TEXT_STREAM, arg,
msg);
PlIOError__(file, f, line, PERMISSION_ERROR_INPUT_TEXT_STREAM, arg, msg);
else
PlIOError__(file, f, line, PERMISSION_ERROR_OUTPUT_TEXT_STREAM, arg,
msg);
PlIOError__(file, f, line, PERMISSION_ERROR_OUTPUT_TEXT_STREAM, arg, msg);
return -1;
}
return sno;
@ -1822,7 +1822,6 @@ static Int get_abs_file_parameter(USES_REGS1) {
Yap_Error(DOMAIN_ERROR_ABSOLUTE_FILE_NAME_OPTION, ARG1, NULL);
return false;
}
void Yap_InitPlIO(void) {
Int i;

View File

@ -458,4 +458,11 @@ char *Yap_MemExportStreamPtr(int sno);
bool Yap_Exists(const char *f);
static inline void freeBuffer(const void *ptr) {
CACHE_REGS
if (ptr == NULL || ptr == LOCAL_FileNameBuf || ptr == LOCAL_FileNameBuf2)
return;
free((void *)ptr);
}
#endif

View File

@ -418,8 +418,10 @@ static xarg *setReadEnv(Term opts, FEnv *fe, struct renv *re, int inp_stream) {
if (fe->cmod == TermProlog)
fe->cmod = PROLOG_MODULE;
if (args[READ_BACKQUOTED_STRING].used) {
if (!setBackQuotesFlag(args[READ_BACKQUOTED_STRING].tvalue))
if (!setBackQuotesFlag(args[READ_BACKQUOTED_STRING].tvalue)) {
free(args);
return false;
}
}
if (args[READ_QUASI_QUOTATIONS].used) {
fe->qq = args[READ_QUASI_QUOTATIONS].tvalue;
@ -651,6 +653,7 @@ static bool complete_processing(FEnv *fe, TokEntry *tokstart) {
else
tp = 0L;
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable);
free(fe->args);
// trail must be ok by now.]
if (fe->t) {
@ -685,7 +688,7 @@ static bool complete_clause_processing(FEnv *fe, TokEntry *tokstart) {
else
v_pos = 0L;
Yap_clean_tokenizer(tokstart, LOCAL_VarTable, LOCAL_AnonVarTable);
free(fe->args);
// trail must be ok by now.]
if (fe->t) {
return (!v_vp || Yap_unify(v_vp, fe->vp)) &&

View File

@ -248,11 +248,11 @@ has_reposition(int sno,
}
char *Yap_guessFileName(FILE* file, int sno, char *nameb, size_t max) {
if (!nameb) {
nameb = malloc(max(256, max));
}
if (!file) {
strcpy(nameb, "memory buffer");
if (!nameb) {
nameb = malloc(max(256, max));
}
if (!file) {
strcpy(nameb, "memory buffer");
return nameb;
}
int f = fileno(file);
@ -639,7 +639,8 @@ static Int cont_stream_property(USES_REGS1) { /* current_stream */
if (IsAtomTerm(args[STREAM_PROPERTY_ALIAS].tvalue)) {
// one solution only
i = Yap_CheckAlias(AtomOfTerm(args[STREAM_PROPERTY_ALIAS].tvalue));
UNLOCK(GLOBAL_Stream[i].streamlock);
free(args)
UNLOCK(GLOBAL_Stream[i].streamlock);
if (i < 0 || !Yap_unify(ARG1, Yap_MkStream(i))) {
cut_fail();
}
@ -648,7 +649,7 @@ static Int cont_stream_property(USES_REGS1) { /* current_stream */
LOCK(GLOBAL_Stream[i].streamlock);
rc = do_stream_property(i, args PASS_REGS);
UNLOCK(GLOBAL_Stream[i].streamlock);
if (IsVarTerm(t1)) {
if (IsVarTerm(t1)) {
if (rc)
rc = Yap_unify(ARG1, Yap_MkStream(i));
if (p == STREAM_PROPERTY_END) {

View File

@ -1,19 +1,19 @@
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: sysbits.c *
* Last rev: 4/03/88 *
* mods: *
* comments: very much machine dependent routines *
* *
*************************************************************************/
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: sysbits.c *
* Last rev: 4/03/88 *
* mods: *
* comments: very much machine dependent routines *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
@ -51,12 +51,6 @@ static const char *expandVars(const char *spec, char *u);
void exit(int);
static void freeBuffer(const void *ptr) {
CACHE_REGS
if (ptr == NULL || ptr == LOCAL_FileNameBuf || ptr == LOCAL_FileNameBuf2)
return;
free((void *)ptr);
}
#ifdef _WIN32
void Yap_WinError(char *yap_error) {
@ -771,7 +765,9 @@ static Int real_path(USES_REGS1) {
if (!rc0) {
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, NULL);
}
return Yap_unify(MkAtomTerm(Yap_LookupAtom(rc0)), ARG2);
bool out = Yap_unify(MkAtomTerm(Yap_LookupAtom(rc0)), ARG2);
freeBuffer(rc0);
return out;
}
#define EXPAND_FILENAME_DEFS() \
@ -797,7 +793,7 @@ static Term do_expand_file_name(Term t1, Term opts USES_REGS) {
xarg *args;
expand_filename_enum_choices_t i;
bool use_system_expansion = true;
char *tmpe = NULL;
const char *tmpe = NULL;
const char *spec;
Term tf;
@ -828,7 +824,7 @@ static Term do_expand_file_name(Term t1, Term opts USES_REGS) {
if (args == NULL) {
return TermNil;
}
tmpe = malloc(YAP_FILENAME_MAX + 1);
tmpe = NULL;
for (i = 0; i < EXPAND_FILENAME_END; i++) {
if (args[i].used) {
@ -836,11 +832,12 @@ static Term do_expand_file_name(Term t1, Term opts USES_REGS) {
switch (i) {
case EXPAND_FILENAME_PARAMETER_EXPANSION:
if (t == TermProlog) {
const char *s = expandVars(spec, LOCAL_FileNameBuf);
if (s == NULL) {
tmpe = expandVars(spec, LOCAL_FileNameBuf);
if (tmpe == NULL) {
free(args);
return TermNil;
}
strcpy(tmpe, s);
spec = tmpe;
} else if (t == TermTrue) {
use_system_expansion = true;
} else if (t == TermFalse) {
@ -861,14 +858,28 @@ static Term do_expand_file_name(Term t1, Term opts USES_REGS) {
}
}
}
free(args);
if (!use_system_expansion) {
const char *o = expandVars(spec, NULL);
if (!o)
return false;
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(o)), TermNil);
tf = MkPairTerm(MkAtomTerm(Yap_LookupAtom(o)), TermNil);
#if _WIN32
if (o != cmd2)
#endif
{
freeBuffer(o);
}
} else {
tf = do_glob(spec, true);
}
if (tmpe
#if _WIN32
&& tmpe != cmd2
#endif
) {
freeBuffer(tmpe);
}
tf = do_glob(spec, true);
return tf;
}
@ -1391,7 +1402,9 @@ static Int true_file_name(USES_REGS1) {
}
if (!(s = Yap_AbsoluteFile(s, LOCAL_FileNameBuf, true)))
return false;
return Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(s)));
bool rc = Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(s)));
freeBuffer(s);
return rc;
}
static Int p_expand_file_name(USES_REGS1) {