[Libreoffice] [LibreOffice] [Patch] Fix for i#108228: Bool has negative sign when used in Writer formula expression
Mattias Johnsson
m.t.johnsson at gmail.com
Wed Dec 8 02:35:43 PST 2010
Fix for http://qa.openoffice.org/issues/show_bug.cgi?id=108228.
The problem behaviour occurs because the boolean expression "true"
evaluates to -1 rather than +1 in formulas in Writer. So if, for
example, you create a table formula in Writer, (Table->Formula) and
enter something like 1+(2==2), it evaluates to 0 rather than 2. It
turns out the reason for this is buried in sbxdef.hxx, where we have
// The numeric values of TRUE and FALSE
enum SbxBOOL { SbxFALSE = 0, SbxTRUE = -1 };
Bizarre, although I have very vague memories of Visual Basic defining
-1 to be true. Is that the reason? If it is supposed to be -1, someone
didn't get the memo, because struct SbxValues in sbxvar.hxx defines a
field "UINT16 nUShort", i.e. an unsigned int, which appears to be
where bools are stored. This is borne out by the code in sbxvalue.cxx
which has
SbxValue::PutBool( BOOL b )
aRes.eType = SbxBOOL;
aRes.nUShort = sal::static_int_cast< UINT16 >(b ? SbxTRUE : SbxFALSE);
Put( aRes );
Weird - the cast changes the -1 to 65535. But wait! If we actually
look at the Put code, we have
SbxValue::Put( const SbxValues& rVal )
...
case SbxBOOL: ImpPutBool( &p->aData, rVal.nInteger ); break;
i.e. we're now back to considering ints rather than uints, and since
SbxValues is defined as a union, the value we've stuffed into it looks
like -1 again if we try to pull an integer out of it. A lot of the
code to do with bools in SbxValues looks like this - it can't make up
its mind if the bool is supposed to be uint or an int, there are casts
everywhere, and it's a miracle it works. Or occasionally doesn't work,
according to the bug.
Lest someone think this bug is correct behaviour, (2==2) currently
gives 1 as expected, not -1; there's an explicit check in the output
in this case to get around the SbxTRUE = -1 thing. Anyway, this patch
fixes the bug, while leaving the whole SbxTRUE = -1 infrastructure
intact. Presumably it should be applied to master rather than 3.3,
since it's hardly an RC blocker :-P
Code contributed under MPL 1.1 / GPLv3+ / LGPLv3+ licenses.
Cheers,
Mattias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Fix-i-108228-bool-has-negative-sign-when-used-in-for.patch
Type: text/x-patch
Size: 784 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20101208/410a4fba/attachment-0001.bin>
More information about the LibreOffice
mailing list