View Issue Details

IDProjectCategoryView StatusLast Update
0000503LDMud 3.3Runtimepublic2018-01-29 22:57
Reporterzesstra Assigned Tolars 
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.3.713 
Fixed in Version3.3.716 
Summary0000503: foreach() with negative integer expression causes "endless" loop
Descriptionforeach(int i: -1) printf("%O",i);
loops as long as there are evaluation ticks left:
"...6503186504186505186506186507186508186509186510186511186512186513186514186515186516186517186518186519186520186521186522186523186524186525186526186527186528186529186530186531186532186533186534186535186536186537186538186539186540186541Error: Too long evaluation. Execution aborted."
Additional InformationI will probably look into the source during the next days and try to write a suitable patch.
TagsNo tags attached.

Activities

2007-04-29 17:36

 

foreach.patch (446 bytes)   
--- ldmud-3.3.714.orig/src/interpret.c	2006-07-10 04:44:33.000000000 +0200
+++ ldmud-3.3.714/src/interpret.c	2007-04-29 23:33:15.000000000 +0200
@@ -15165,6 +15165,8 @@
         if (arg->type == T_NUMBER)
         {
             count = arg->u.number;
+            if (count<0)
+              ERRORF(("foreach() got %ld, expected a positive integer.\n",count));
             vars_required = 1;
         }
         else if (arg->type == T_STRING)
foreach.patch (446 bytes)   

zesstra

2007-04-29 17:47

administrator   ~0000531

Ok, this is a tiny patch for interpret.c which just checks 'count' in CASE(F_FOREACH) if the argument is of type T_NUMBER. If 'count' is negative, it throws an error via ERRORF().
One could argue that the original behaviour is not a bug and just a possibility to iterate over a larger integer range than 0 - MAX_INT. But I think, that isn't a very realistic scenario. Most often, a negative integer as range expression is just not intended and therefore I would prefer to not accept it.
BTW: Another possibility is to silently correct a negative 'count' to 0 and only issue a warning, but I would definitely prefer a real error as this situation anyway leads to a 'too long evalution' error.

2007-04-29 18:01

 

foreach2.patch (460 bytes)   
--- ldmud-3.3.714.orig/src/interpret.c	2006-07-10 04:44:33.000000000 +0200
+++ ldmud-3.3.714/src/interpret.c	2007-04-29 23:55:34.000000000 +0200
@@ -15165,6 +15165,8 @@
         if (arg->type == T_NUMBER)
         {
             count = arg->u.number;
+            if (count<0 && !use_range)
+              ERRORF(("foreach() got %ld, expected a positive integer.\n",count));
             vars_required = 1;
         }
         else if (arg->type == T_STRING)
foreach2.patch (460 bytes)   

lars

2007-10-07 00:40

reporter   ~0000560

Yup, makes sense.

Issue History

Date Modified Username Field Change
2007-04-25 19:02 zesstra New Issue
2007-04-29 17:36 zesstra File Added: foreach.patch
2007-04-29 17:47 zesstra Note Added: 0000531
2007-04-29 18:01 zesstra File Added: foreach2.patch
2007-10-07 00:40 lars Status new => resolved
2007-10-07 00:40 lars Fixed in Version => 3.3.716
2007-10-07 00:40 lars Resolution open => fixed
2007-10-07 00:40 lars Assigned To => lars
2007-10-07 00:40 lars Note Added: 0000560
2010-11-16 10:42 lars Source_changeset_attached => ldmud.git master 3bf94a56
2018-01-29 19:59 lars Source_changeset_attached => ldmud.git master 3bf94a56
2018-01-29 22:57 lars Source_changeset_attached => ldmud.git master 3bf94a56