Jan,
Thank you so much for providing swipl -d chk_secure, without it, it would have been almost impossible to debug an error in my foreign code!
Two warnings for all those who are doing non deterministic foreign predicates:
- don’t write code that references the
term_targuments outside of theswitchstatement which checks if the call isPL_FIRST_CALL,PL_REDOorPL_PRUNED.
2) don’t use theterm_treferences inside thePL_PRUNEDcase.
Longer explanation:
If an exception is thrown (after the foreign predicate returned with one of PL_retrys), the term_t arguments are not valid anymore, and the the function is called again with control_t call type set to PL_PRUNED (this is needed to cleanup memory).
If you have code that uses the term_ts it will crash the system, eventually causing the garbage collector will bail out with a segfault.