protect atoms in SWI interface.

This commit is contained in:
Vitor Santos Costa 2012-01-17 12:37:29 +00:00
parent f2dc53a53e
commit db619fa81f
2 changed files with 18 additions and 11 deletions

View File

@ -293,39 +293,43 @@ _PL_unify_string(term_t t, word w)
word lookupAtom(const char *s, size_t len)
{
YAP_Atom at;
/* dirty trick to ensure s is null terminated */
char *st = (char *)s;
st[len] = '\0';
if (len >= strlen(s)) {
return (word)YAP_LookupAtom(s);
at = YAP_LookupAtom(st);
} else {
char * buf = PL_malloc(len+1);
word out;
if (!buf)
return 0;
strncpy(buf,s,len);
out = (word)YAP_LookupAtom(buf);
at = YAP_LookupAtom(buf);
PL_free(buf);
return out;
}
}
Yap_AtomIncreaseHold(at);
return (word)at;
}
atom_t lookupUCSAtom(const pl_wchar_t *s, size_t len)
{
YAP_Atom at;
if (len >= wcslen(s)) {
return (atom_t)YAP_LookupWideAtom(s);
at = YAP_LookupWideAtom(s);
} else {
pl_wchar_t * buf = PL_malloc((len+1)*sizeof(pl_wchar_t));
word out;
if (!buf)
return 0;
wcsncpy(buf,s,len);
out = (word)YAP_LookupWideAtom(buf);
at = YAP_LookupWideAtom(buf);
PL_free(buf);
return out;
}
Yap_AtomIncreaseHold(at);
return (atom_t)at;
}

View File

@ -682,6 +682,7 @@ X_API atom_t PL_new_atom_wchars(size_t len, const wchar_t *c)
}
}
at = AtomToSWIAtom(at0);
Yap_AtomIncreaseHold(at0);
YAP_FreeSpaceFromYap(nbf);
} else {
char *nbf;
@ -704,8 +705,8 @@ X_API atom_t PL_new_atom_wchars(size_t len, const wchar_t *c)
return 0;
}
}
Yap_AtomIncreaseHold(at0);
at = AtomToSWIAtom(at0);
Yap_AtomIncreaseHold(at0);
YAP_FreeSpaceFromYap(nbf);
}
return at;
@ -1099,8 +1100,8 @@ X_API int PL_unify_atom_nchars(term_t t, size_t len, const char *s)
return FALSE;
}
}
Yap_AtomIncreaseHold(catom);
free(buf);
Yap_AtomIncreaseHold(catom);
cterm = MkAtomTerm(catom);
return YAP_Unify(Yap_GetFromSlot(t PASS_REGS),cterm);
}
@ -1337,6 +1338,7 @@ X_API int PL_unify_wchars(term_t t, int type, size_t len, const pl_wchar_t *char
return FALSE;
}
}
Yap_AtomIncreaseHold(at);
chterm = MkAtomTerm(at);
}
break;
@ -1418,6 +1420,7 @@ LookupMaxWideAtom(size_t n, wchar_t *s)
return NULL;
}
}
Yap_AtomIncreaseHold(catom);
Yap_FreeAtomSpace((ADDR)buf);
return catom;
}