Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2003-06-25
15:18 • Ticket [758110ffff] patch for 'dict info {}' status still Closed wit... artifact: 4f1c88ae43 user: dkf
03:58 • Ticket [758110ffff]: 4 changes artifact: d0ace9cd30 user: hgiese
02:59 • Closed ticket [758110ffff]. artifact: 30c7640a67 user: dkf
2003-06-23
22:51 • Ticket [758110ffff]: 4 changes artifact: 0f697b36bc user: nobody
22:07 • Ticket [758110ffff]: 1 change artifact: 4771f1f173 user: dkf
22:07 • Ticket [758110ffff]: 1 change artifact: 8e8db3eafa user: dkf
2003-06-20
19:07 • New ticket [758110ffff]. artifact: f7b0109757 user: hgiese

patch for 'dict info {}'

Closed / Fixed758110ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-06-20 19:07:49
Hello out there,
the command
    dict info ""
leads to a crash in my Borland generated Tcl 8.5a0 (on
Win98).

Here is what happens on my machine:
- The first time around in DictInfoCmd the TclObj in
question is not of
  type dictionary and so it gets converted via a call
to SetDictFromAny.
- Then the variable 'Dict *dict' is properly
initialised. Inspecting it
  I see that its 'table' element has its element
'numEntries' set to '0'
  - reasonable.
- But then within Tcl_HashStats one calculates
    average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;
  with tablePtr->numEntries being '0' -> BOOM.

I would suggest the following change in function
"Tcl_HashStats" (file
tclHash.c):

Put the line
        average +=
(tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;
into a conditional like
        /*
         * On machines not supporting "automatic NAN
handling" or such, we
         * really should test for the denominator == 0
         */
        if ( tablePtr->numEntries )
            average +=
(tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;

This will return a value of 0.0 for the average - which
is maybe not
exactly right but a reasonable value for an empty
dictionary.

Best regards
Helmut Giese
dkf Ticket [758110ffff]: 1 change [History diff]2003-06-23 22:07:30
dkf Ticket [758110ffff]: 1 change [History diff]2003-06-23 22:07:31
nobodyplain2003-06-23 22:51:26
Logged In: NO 

Hi Donal,
meanwhile I got some more information. It seems possible to
turn off floating point exceptions, thus (maybe) getting a
NAN result like you do on Windows, too - haven't tested it yet.
This leads me to the question: How does it run on Windows
without the above patch? Returns a NAN? Throws an exception?
It could be that VC++ leaves (floating point) exceptions
disabled (appears to be the default) and Borland does not,
so a better patch might be to have Borland behave like VC -
which would mean a different patch and would eliminate the
need for the one above.

Maybe let it rest for a couple of days, 'til I got time to
further look into this.
Best regards
Helmut Giese
dkfplain2003-06-25 02:59:05
Logged In: YES 
user_id=79902

FP exceptions are a separate matter, but there's no real
reason for that code to divide by zero in the first place.
hgieseplain2003-06-25 03:58:30
Logged In: YES 
user_id=522169

Hi Donal,
I was almost ready to withdraw the patch: 
1) I figured you were content of getting a NAN for 'avg'.
2) Meanwhile I'm a bit further ahead on the way to Borland &
Tcl Nirvana - I don't GP at this test, so it's not a ral
issue for me anymore.
Of course, if you think that any division by zero should be
avoided - well, put in the conditional.
For me, I consider this (tiny) issue closed - got bigger
problems with Borland :(
Best regards
Helmut Giese
dkfplain2003-06-25 15:18:55
Logged In: YES 
user_id=79902

Bah.  There was just no need to ever divide by zero anyway
in that code.  It had the feeling of a case never considered
by the original author (and that code is one of the oldest
parts of Tcl, dating from before the CVS archive.  IIRC, it
was exactly like that in 7.4b1 which was the first version
of Tcl I've ever looked at...)
Log in (anonymous is fine) to add a comment.