[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4762: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4764: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4765: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4766: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
newlispfanclub.alh.net • View topic - proper way to detect NULL char* on imported function?

proper way to detect NULL char* on imported function?

Q&A's, tips, howto's

proper way to detect NULL char* on imported function?

Postby TedWalther » Thu May 07, 2015 10:14 pm

Last edited by TedWalther on Thu May 07, 2015 10:39 pm, edited 1 time in total.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: proper way to detect NULL char* on imported function?

Postby TedWalther » Thu May 07, 2015 10:15 pm

After a bit more testing, I can reduce it to this: if an extended libffi imported function has a return type of char*, and it returns NULL when it is called, newlisp segfaults.

Perhaps the least surprising response to this is to return a "nil", since "char*" does map to a newlisp string type.

I verified that (get-string NULL) also segfaults, but perhaps this is the correct behavior. It is conceivable that memory permissions could be set such that you could legitimately try to grab the byte at memory location 0.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: proper way to detect NULL char* on imported function?

Postby Lutz » Fri May 08, 2015 3:28 pm

I rather would like to leave things as they are as there are to many implications for other parts of the code like structure packing and unpacking.

If a NULL pointer can be expected as a return value use void* instead as a return type and do the conversion of a valid non zero returned string pointer using get-string.

In the same fashion when NULL is expected to be passed for a char* parameter use void* as parameter type instead.

void* as parameter type, already takes newLISP strings besides numbers and automatically converts them to addresses and this is mentioned already in the docs.

I will add something to the documentation to make all this clearer.

ps: I could add an error message when a libffi char* return type function returns NULL.
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: proper way to detect NULL char* on imported function?

Postby TedWalther » Fri May 08, 2015 8:01 pm

Lutz... anytime you deal with pointers, no matter what type, NULL can come up, and not necessarily be an error. It is the standard "end of list" marker. Even with documentation and an error code instead of a segfault, this is still very surprising behavior.

Can we do a conference offline by phone so you can fill me in more on the things that would be affected by making (import char*) return nil instead of an error, for NULL? I propose the phone conference because I communicate and process information much more rapidly by voice, and language barrier will not be an issue for us. I promise.

I'd like to get a better feel for how I can assist; I've been requesting a lot lately, and I'd like to become more of a bug-fixer and contributor, rather than bug-reporter and requestor.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: proper way to detect NULL char* on imported function?

Postby Lutz » Fri May 08, 2015 8:20 pm

If you expect 0 then either use void* and check for 0/NULL before calling get-string or if using char* use catch. I don't want to start a nil/NULL confusion. Also, when using libffi, nil is already used as return value for void return.
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron