cannot export char U+0000 in nul-terminated string buffer

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

cannot export char U+0000 in nul-terminated string buffer

Post by yeldarb » Wed Dec 27, 2017 5:39 pm

I'm using a browser widget to display formatted documents. I frequently get an error:
execution error at line 20 (LCB Error in file browser.lcb at line 409: cannot export char U+0000 in nul-terminated string buffer) near "foreign", char 47
I can't seem to track down the issue. It's not consistent. I've run the html through online cleaners, copied from TextEdit, and it's happens with several documents and none of them have any odd characters in them.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2792
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: cannot export char U+0000 in nul-terminated string buffer

Post by richmond62 » Thu Dec 28, 2017 9:40 am

That's odd.

U+0000 is the first null character in the Unicode "universe":
Screen Shot 2017-12-28 at 10.34.28 am.png
Screen Shot 2017-12-28 at 10.34.28 am.png (33.1 KiB) Viewed 673 times
it will not show up as an "odd" character, it won't show up at all.

Personally I'd run it through a routine that specifically looks for U+0000 and replaces it
with something "boring" such as a SPACE (U+0020 = decimal 32).
nullHtml.png
nullHtml.png (8.9 KiB) Viewed 670 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2792
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: cannot export char U+0000 in nul-terminated string buffer

Post by richmond62 » Thu Dec 28, 2017 12:33 pm

Oh, Blast, didn't really register the problem is NOT the presence of a NULL char Ding An Sich,
but a null-terminated string buffer, or C-String:
Gstring.jpg
G-String
Gstring.jpg (2.81 KiB) Viewed 657 times
https://en.wikipedia.org/wiki/Null-terminated_string

So, possibly, that's a problem with LCB's string variables:

"Many attempts made handling less error prone; moreso, making C string handling less error prone. One strategy is to add safer, encompassing functions such as strdup and strlcpy, whilst deprecating the use of unsafe functions such as gets. Another is to add an object-oriented wrapper around C strings so that only safe calls can be done."

If LCB string variables are null-terminated then the NULL termination chars have to be
stripped out or replaced with another char before an attempt is made to export them.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2792
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: cannot export char U+0000 in nul-terminated string buffer

Post by richmond62 » Thu Dec 28, 2017 12:41 pm

Digging around:

"U+0000 NUL is forbidden in all versions of HTML. Although HTML5 CR specifies error handling rules for it in HTML parsing, NUL is known to cause problems in browsers. It might be argued that HTML rules do not apply to \0, which is just two printable Ascii characters as far as HTML is considered and gets interpreted as NUL by JavaScript interpreters. But using NUL that way in HTML documents violates at least the spirit of HTML and should not be expected to work reliably."

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2792
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: cannot export char U+0000 in nul-terminated string buffer

Post by richmond62 » Thu Dec 28, 2017 3:40 pm

On 28/12/2017 3:37 pm, Mark Waddingham via use-livecode wrote:
> In LCB a string is a string and a number is a number - there's an operator which tests whether a string 'looks like' a number and can optionally convert it.
>
> Strings in LCB are the same as in LCS - counted and not nul terminated (so they can contain the NUL 'char' - i.e. Code 0). It does have 'bridging' string types which are nul terminated - they are there to support calling a foreign functions which use such strings.
>
> Warmest Regards,
>
> Mark.

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 996
Joined: Thu Apr 11, 2013 11:27 am

Re: cannot export char U+0000 in nul-terminated string buffer

Post by LCMark » Thu Dec 28, 2017 4:22 pm

@yeldarb: What version of LiveCode are you using? (Just so I can match up the error line to the correct version of the browser widget source file).

I'm guessing this is happening when a property is being set - my guess that it is 'the htmlText', and the string you are passing to 'the htmlText' contains a NUL byte. The reason this is an issue is that the browser widget uses C-strings to pass to the underlying browser object.

You could try:

Code: Select all

replace numToChar(0) with empty in tString
set the <prop> to tString
This will remove the NUL bytes from tString - and so (hopefully) get rid of the problem.

yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

Re: cannot export char U+0000 in nul-terminated string buffer

Post by yeldarb » Thu Dec 28, 2017 6:29 pm

LC Indy 8.1.7

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 996
Joined: Thu Apr 11, 2013 11:27 am

Re: cannot export char U+0000 in nul-terminated string buffer

Post by LCMark » Thu Dec 28, 2017 6:49 pm

@yeldarb: Thanks - I've checked and that line is the call to the C function which 'sets a browser string property' - the C API uses a UTF-8 NUL terminated string - so you need to be careful to strip NULs from strings you use to pass to the browser via property setters in LiveCode Script.

(LCB handles the details of conversion internally, but it doesn't try to do things that don't make sense - and trying to create a NUL terminated string from a string which contains a NUL doesn't make sense - hence the error).

yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

Re: cannot export char U+0000 in nul-terminated string buffer

Post by yeldarb » Sun Dec 31, 2017 7:56 pm

So I added the suggested line:

Code: Select all

replace numToChar(0) with empty in html
   
and that seems to have fixed the problem, thanks!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”