expr parses numbers before function names
Closed / Fixed3401704fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-31 16:22:07
You cannot define expr functions with names that begin with numbers (including "inf" and "nan"). The expression parser looks for numbers before looking for function names. For example,
expr {2*nancy(5)}
will throw the following error:
missing operator at _@_
in expression "nan_@_cy(5)"
Function names beginning with a digit have the same problem.
Note: it doesn't matter whether the tcl::mathfunc::nancy command is actually defined or not.
Referenced by1
63df456f61New ticket [1e66ba3d49] expr cannot call math functions named eq/ne/in/ni or starting with them (in_range, eq_zero, ...). ticket2026-10-17 10:51:11
duquetteplain2011-08-31 23:22:44
Gave this a high priority, per DKF on c.l.t.
msofer Ticket [3401704fff]: 1 change
[History diff]2011-08-31 23:24:06
ferrieuxplain2011-09-01 00:15:04
It looks like TclParseNumber tries to mimic strtod. Quoting the manpage: The strtod(), strtof(), and strtold() functions convert the initial portion of the string pointed to by nptr to double, float, and long double representation, respectively. The attached patch fixes the issue by detecting trailers after Nan and Inf[inity]. Trailers currently defined as [0-9A-Za-z_], should be generalized (is there a function for that ?). In case they are detected, the number parser bails out, and [expr] reports the correct "invalid bareword nancy" or "invalid command name ::tcl::mathfunc::nancy depending on the context.
ferrieuxplain2011-09-01 00:15:42
File Added - 422482: exprnan.patch
dgpplain2011-09-01 00:30:52
Can you commit the patch to a branch bug-3401704 for more convenient review please? Thanks!
ferrieuxplain2011-09-01 00:50:47
Done :)
ferrieuxplain2011-09-01 01:26:06
File Deleted - 422482:
ferrieuxplain2011-09-01 01:27:15
Branch bug-3401704 now has the cleaner 'isalnum' predicate instead of hard-coded tests. Also includes a new test case expr-22.10 Removing attached patch now out of sync.
dgpplain2011-09-01 01:39:55
Oh. You revised TclParseNumber()? Not at all what I was expecting. My unexamined opinion is that the flaw is in the parsing of expressions, not numbers. Will look it over though.
ferrieuxplain2011-09-01 01:43:19
Yes. Rationale: ParseLexeme uses TclParseNumber pretty early, as a tokenization first pass. So if I'd fixed the caller, I'd have gotten "inbalid bareword 'cy'". So I had to fix the callee.
dgpplain2011-09-01 01:49:15
Yes but TclParseNumber has other callers too.
FWIW, 8.6b2 produces this:
% expr {1eq1}
1
% expr {infeqinf}
invalid bareword "eqinf"
in expression "inf_@_eqinf";
should be "$eqinf" or "{eqinf}" or "eqinf(...)" or ...
% expr {infeq inf}
1
ferrieuxplain2011-09-01 02:41:19
With the patch this becomes:
% expr {infeqinf}
invalid bareword "infeqinf"
in expression "infeqinf";
do you prefer the old behavior ? FWIW I don't :}
ferrieuxplain2011-09-01 04:13:21
As another example: string is double -failindex x nancy formerly returned 3 in $x, now zero. Same remark. Do you have counter examples ? A true regression ?
dgpplain2011-09-01 04:33:51
Just for the record, this issue is not a regression.
% info patch
7.6p2
% expr {nancy(2)}
unknown math function "cy"
% expr {influence(1.2)}
unknown math function "luence"
similar results all along the way.
ferrieuxplain2011-09-01 04:35:36
I went through all call sites of TclParseNumber (in the core; not bothered about extensions). What I gather from this is: - there are many call sites, so fixing them would be tricky/unmaintainable - fixing the callee only improves existing error cases
ferrieuxplain2011-09-01 04:39:43
IP - Comment Removed: 81.53.167.132
ferrieuxplain2011-09-01 04:39:48
IP - Comment Removed: 81.53.167.132
ferrieuxplain2011-09-01 04:39:50
IP - Comment Removed: 81.53.167.132
kennykbplain2011-09-01 04:41:44
It seems sensible that 'infirmity' or 'nancy' should not be considered numbers. And the only obvious use case that I see breaking is simply that it would change error behaviour for things like screen metrics: [expr {1/0}]cm] would now complain that Infcm isn't a number, rather than being an infinite number of centimetres.
dgpplain2011-09-01 05:11:33
On trunk:
% scan infernal %g%s
Inf ernal
On review branch:
% scan infernal %g%s
{} {}
ferrieuxplain2011-09-01 15:41:00
To summarise why I think revising T_PN is okay:
T_PN's contract can be described by a single regexp not very far from (simplifying on the actual float syntax):
^[+-]?(
[0-9.e]+
|
Inf(inity)?
|
Nan(\([0-9af]+\))?
)
Note that due to the greediness of '+', adding a positive lookahead of [^0-9] after the floating-point part is a no-op:
^[+-]?(
[0-9.e]+ (?=[^0-9])
|
Inf(inity)?
|
Nan(\([0-9af]+\))?
)
This is the current trunk status.
Now the patch just adds 2 more lookaheads:
^[+-]?(
[0-9.e]+ (?=[^0-9])
|
Inf(inity)? (?=[^0-9A-Za-z_])
|
Nan(\([0-9af]+\))? (?=[^0-9A-Za-z_])
)
That's it. No dirty hack, just a slight bend of the accepted regular language. And that bend is justified, across all callers of T_PN, by the fact that Nancy and Infinitesimal being parsed as Nan+cy and Inf+initesimal are utterly meaningless. If you want to flag this as POTENTIAL INCOMPATIBILITY, please do, but it's a very very tiny one.
While doing the equivalent fix only in T_PE would IMO break symmetry. People doing script emulations of one of the primitives with the others would start noting inconsistencies !
dgpplain2011-09-02 03:43:35
see dgp-3401704 branch
ferrieuxplain2011-09-02 04:25:20
Translation: it's a NO. OK , won't fight.
dgpplain2011-09-07 00:50:16
Earlier attempt was a failed experiment. See new dgp-3401704 branch for new attempt.
dgpplain2011-09-08 01:20:43
allow_comments - 1 fixed for 8.5.11, 8.6b3.
Log in (anonymous is fine) to add a comment.