Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-09-01
20:56 • Closed ticket [cd61f085da]: tcl terminates silently with invalid expr underscore... artifact: 6f52983718 user: griffin
19:32 • Ticket [cd61f085da]: 3 changes artifact: a055e0a9b4 user: juliannoble2
06:41 • Ticket [cd61f085da]: 3 changes artifact: 37176f26ed user: juliannoble2
03:41 • Ticket [cd61f085da]: 3 changes artifact: fd7c894e6c user: griffin
2023-08-31
08:00 • Ticket [cd61f085da]: 3 changes artifact: 582d416722 user: juliannoble2
2023-08-30
12:33 • Ticket [cd61f085da]: 3 changes artifact: 1a056e905a user: juliannoble2
2023-08-29
17:47 • Ticket [cd61f085da]: 3 changes artifact: f77e0fdcd8 user: juliannoble2
16:18 • Ticket [cd61f085da]: 3 changes artifact: 090de37c09 user: juliannoble2
14:36 • Ticket [cd61f085da]: 3 changes artifact: 6a95f9c177 user: juliannoble2
14:30 • Ticket [cd61f085da]: 3 changes artifact: 3d7e9c18df user: juliannoble2
02:01 • Ticket [cd61f085da]: 3 changes artifact: f638f27bea user: griffin
2023-08-28
10:12 • Ticket [cd61f085da]: 3 changes artifact: 23d811aad4 user: juliannoble2
09:56 • Ticket [cd61f085da]: 3 changes artifact: 4fcc78a5bd user: jan.nijtmans
09:29 • Ticket [cd61f085da]: 3 changes artifact: bc8eb3c132 user: chrstphrchvz
09:27 • Ticket [cd61f085da]: 3 changes artifact: 061e388bf4 user: juliannoble2
09:06 • New ticket [cd61f085da]. artifact: d081f3a5ee user: juliannoble2

tcl terminates silently with invalid expr underscore syntax and radix prefix

Closedcd61f085da52b3384388c8ef780f53d8c55ead4fCreated 3 years agoUpdated 3 years ago
Descriptionplain2023-08-28 09:06:22
With a script or from tcl shell - Tcl version 8.7 or 9

Execute the command

expr {0x_}
or
expr {0d_} 
or 
expr {0b_}
or
expr {0o_}

The interpreter will silently exit.

Expected: an error to be raised.
juliannoble2plain2023-08-28 09:27:26
This also happens if a character outside of the range expected by the radix follows the underscore
e.g
expr {0d_a12}
expr {0b_21010}
expr {0x_GFF}
expr {0o_876}
chrstphrchvzwiki2023-08-28 09:29:40

I tried this and got a stack overflow.

% expr {0x_}
AddressSanitizer:DEADLYSIGNAL
=================================================================
==36708==ERROR: AddressSanitizer: stack-overflow on address 0x7ff7bac29fd0 (pc 0x000105031987 bp 0x7ff7bac2a030 sp 0x7ff7bac29fd0 T0)
    #0 0x105031987 in __sanitizer::StackDepotBase<__sanitizer::StackDepotNode, 1, 20>::Put(__sanitizer::StackTrace, bool*)+0x17 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x106987)
    #1 0x104f2f28c in __asan::Allocator::Allocate(unsigned long, unsigned long, __sanitizer::BufferedStackTrace*, __asan::AllocType, bool)+0x3cc (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x428c)
    #2 0x104f2ee98 in __asan::asan_malloc(unsigned long, __sanitizer::BufferedStackTrace*)+0x28 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x3e98)
    #3 0x1050087a0 in wrap_malloc+0x100 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0xdd7a0)
    #4 0x1066fca54 in TclpAlloc tclAlloc.c:699
    #5 0x1067fb224 in Tcl_Alloc tclCkalloc.c:1038
    #6 0x106f1ef5c in ParseLexeme tclCompExpr.c:2090
    #7 0x106f2014f in ParseLexeme tclCompExpr.c:2127
    #8 0x106f2014f in ParseLexeme tclCompExpr.c:2127
    #9 0x106f2014f in ParseLexeme tclCompExpr.c:2127
⋮
    #254 0x106f2014f in ParseLexeme tclCompExpr.c:2127

SUMMARY: AddressSanitizer: stack-overflow (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x106987) in __sanitizer::StackDepotBase<__sanitizer::StackDepotNode, 1, 20>::Put(__sanitizer::StackTrace, bool*)+0x17 ==36708==ABORTING

jan.nijtmans Ticket [cd61f085da]: 3 changes [History diff]2023-08-28 09:56:11
juliannoble2plain2023-08-28 10:12:28
This doesn't appear to be exclusive to the radix prefixes
Also with malformed scientific notation:

expr {2.1e_}
griffinplain2023-08-29 02:01:23
Fix on branch [bug-cd61f085da]. 
Included a change to disallow 123_._456, i.e., underscore adjacent to '.'.  
Or, in other words, there must be a digit on both sides of an underscore.
I need to clean up the error messages. 
They don't all accurately reflect the true error.
juliannoble2plain2023-08-29 14:30:38
I checked out branch bug-cd61f085da
It seems more robust.

I see you disallowed multiple underscores in a row. I confess to being a little disappointed - as my concerns about the syntax leaking probably don't distinguish between 1_000  vs 'non standard' groupings like 10_00  or novel spacings like 10___00.  If it's going to have any underscores the care has to be taken anyway - and I actually liked the less conservative approach in that minor regard.
The comments about creative freedom on the core mailing list were interesting.

e.g the following was mentioned:
#          foo   bar   snafu
set list {
           54_____6_____000
           42_____9_____001
}

In combination with DKFs tip 677  https://core.tcl-lang.org/tips/doc/trunk/tip/677.md
, giving some scoped immutability to variables - I thought this would be very nice to retain.

Anyway - perhaps implementation issues are the decider.

I do feel however there is a remaining inconsistency that may cause errors when editing code:

% expr 2.1e_2
210.0
% expr 2.1e_+2
invalid character "."
in expression "2.1e_+2"
%
% expr 2.1e+_2
210.0

If the underscore is allowed after the e when there is no +/- then it should be allowed in the presence of the + or - too - otherwise someone simply editing to throw in a + or - of an already underscored value will be surprised.
juliannoble2plain2023-08-29 14:36:55
also - the fix
disallows  1_._1  
disallows 1_.1
allows 1._1

I think that 3rd case should be disallowed.
juliannoble2plain2023-08-29 16:18:17
There's another edge case around scientific notation.
Underscore seems to be allowed before, or after the e but not both.

Perhaps the rule should be underscores between digits or after a radix specifier 0x etc  - but not next to the e (or +/-) in sci notation.
Your insights into what makes implementation less curly may come up with a different rule - but hopefully something consistent and easily understandable is practical.

% expr 1.0e_2
100.0
% expr 1.0_e2
100.0
% expr 1.0_e_2
invalid character "."
in expression "1.0_e_2"
juliannoble2plain2023-08-29 17:47:55
Just as a further reason why I think the rules for underscores need to be very simple and understandable and why the underscores shouldn't be allowed adjacent to e|E:


#1
% set x 1.e_2
1.e_2
% expr {$x < 100}
0

#2
% set x 1.e_+2
1.e_+2
% expr {$x < 100}
1


#2 gets us right back to accidental string comparison (though this example is just an artifact of the current state of the fix)

if you allow 1.0_e2  but then disallow 1._e2  to be consistent with disallowing 1._1 & 1_.1 then this also risks accidental string comparison for those assuming 'before e|E is always ok'.

If the current fix allowing 1._1 is intentional - then the risk is misinterpretation of the rules to think 1_._1 and/or 1_.1 are ok - as well as the previously mentioned surprise for those splitting on the dot and getting a string in part b even after having already filtered out sci notation.

By disallowing anywhere but in between digits and perhaps after radix spec - it would seem to alleviate this risk.
juliannoble2plain2023-08-30 12:33:52
From playing with this bugfix version - it's become clearer to me that the bigger problem is minor 'numeric whitespace' editing (i.e changing internal underscores)
causing accidental string comparisons in later expressions which could go unnoticed depending on the data.

Because 1.e2  is legitimate notation - rules around underscore not being allowed near the . or the e or the +|- in scientific notation become too complex and prone to accidental string comparison as in my previous example.

My concern about people splitting on .  in 0_._0  (or on eE )  are less important - and best addressed by documenting something along the lines of: Use 'string map {_ ""} $num'  prior to doing any string analysis of a number or its components.

I think the initial version had this right in being lenient about internal underscores  - except for the bug reported in this ticket.
juliannoble2plain2023-08-31 08:00:13
Tested checkin  9a2043019b

Your new rule - stated in the comments as 'for an integer value'; essentially that a numeric whitespace must begin and end with a digit from the relevant base ; is I think a nice clear simple rule. That 0x_a1 0b_1 etc are now disallowed seems reasonable and probably makes code analysis/review easier.

The current state of the fix seems not quite there as it still disallows
0.000_001 
0.000_001e0_2
but allows
0.000001e0_2

I presume the same rule goes for the RHS of floats and these should all be allowed.
griffinmarkdown2023-09-01 03:41:20

All the reported failures have been fixed, as well as others I've discovered. This fix is on branch bug-cd61f085da

The implementation reflects the following description of numeric white-space:

The definition for numeric white-space is simplified to: 1 or more '_' characters may be placed between digits only.

Numeric white-space cannot be:

  • at the beginning of a number

  • before or after a '+' or '-'

  • before or after a decimal point '.'

  • before or after an exponent 'e' or 'E' symbol

  • before, after, or in between a base specifier 0b, 0o, 0d, or 0x

Legal examples:

    0x1_2__34___56
    2_0.3_4e+1_5

Illegal examples:

    _0_x_123456_
    _20_._34_e_+_15_
juliannoble2plain2023-09-01 06:41:53
Only problem I could find is octal with 2 or more underscores in a row gives an (invalid octal number?) error.
juliannoble2plain2023-09-01 19:32:57
Nice tight spec. This monkey-powered keyboard couldn't break it.
griffinplain2023-09-01 20:56:55
Fixed with 5fea51151f
Log in (anonymous is fine) to add a comment.