Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

TonyV

Crash in Wilbur's string text (Fix included in post)

Recommended Posts

TonyV    9

I was plaing Don't Starve: Shipwrecked today, and it crashed as I started hearing hound noises.  I've attached a picture below of the crash log.

D1B6B65842408AD2EC45355ABE194C6C6EE68EF3

After doing a little investigating, I found the following code responsible for the crash:

60      local function NumInRange(num, min, max)
61          return (num <= max) and (num > min)
62      end

...

90      local string_space = function()
91          local c = math.random()
92          local str =
93          (NumInRange(c, 0.4, 1) and " ") or
94          (NumInRange(c, 0.3, 0.4) and ", ") or
95          (NumInRange(c, 0.2, 0.3) and "? ") or
96          (NumInRange(c, 0.1, 0.2) and ". ") or
97          (NumInRange(c, 0, 0.1) and "! ")
98          if c <= 0.3 then
99              STRING_STATE = "START"
100         else
101             STRING_STATE = "MID"
102         end
103         return str
104     end
105     
106     local length = math.random(6)
107     local str = ""
108     for i = 1, length do
109         str = str..string_start()..string_end()
110         if i ~= length then
111             str = str..string_space()
112         end
113     end

The problem, I think, is the rare but mathematically possible situation in which math.random() returns exactly 0.0.  Note that according to the lua documentation, math.random() returns a number between [0, 1); that is, possibly including 0, and up to (but not including) 1.  Because the NumInRange function checks for the value being greater than the minimum, if math.random() generates exactly 0.0, the long boolean expression assigned to str will result in a boolean false value, which gets returned to the concatenation operation, causing a crash.

To try to validate this, I inserted the following just after the math.random() assignment:

92          if c > 0.5 then c = 0.0 end

Sure enough, when I started Don't Starve again, after clicking a couple of times on some items for Wilbur to inspect, the game crashed with the same error when c was set to this valid value possibly returned by math.random().  To test a fix for the issue, I added one more test to the end of the boolean expression so that it became:

93          local str =
94          (NumInRange(c, 0.4, 1) and " ") or
95          (NumInRange(c, 0.3, 0.4) and ", ") or
96          (NumInRange(c, 0.2, 0.3) and "? ") or
97          (NumInRange(c, 0.1, 0.2) and ". ") or
98          (NumInRange(c, 0, 0.1) and "! ") or
99          "*"

Notice the added or "*" at the end.

When I re-entered the game and tested it out, sure enough, I was getting asterisks (*) in many of the strings generated for Wilbur.

98AE7CA0A756624C50AD961DD1C03D646C35188F

So the test fix worked.  To fix the script so that it worked they way it was supposed to, I removed the test and assignment for c to force it to be 0.0 and removed the asterisk, using the exclamation point as the default value instead.  The boolean expression on line 97 now looks like this:

92          local str =
93          (NumInRange(c, 0.4, 1) and " ") or
94          (NumInRange(c, 0.3, 0.4) and ", ") or
95          (NumInRange(c, 0.2, 0.3) and "? ") or
96          (NumInRange(c, 0.1, 0.2) and ". ") or
97          "! "

Note that the NumInRange check has been omitted, causing the expression to evaluate to "! " if c is any number outside the given range.  That should completely fix the issue once and for all.

Hope this helps!

Share this post


Link to post
Share on other sites
JanH    5,206

Thank you for the report; we will look into this.

Share this post


Link to post
Share on other sites