Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix invalid array index generation in fml.coffee #1312

Merged
merged 1 commit into from

2 participants

@kppullin

Swapped the usage of Math.round with Math.floor to prevent the generation of an invalid array index and subsequent exception.

Example:

Assume Math.random() returns .9 and the array length is 5.

Math.round(.9 * 5) yields 5, exceeding the bounds of the array and triggers an exception.
Math.floor(.9 * 5) yields 4, a valid index and the script works as expected.

@kppullin kppullin Fix invalid array index generation in fml.coffee
Swapped the usage of `Math.round` with `Math.floor` to prevent the generation of an invalid array index and subsequent exception.


Example:

Assume Math.random() returns `.9` and the array length is 5.

`Math.round(.9 * 5)` yields 5, exceeding the bounds of the array, and triggers an exception.
`Math.floor(.9 * 5)` yields 4, a valid index and the script works as expected.
c44c99a
@technicalpickles technicalpickles merged commit f78097d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2014
  1. @kppullin

    Fix invalid array index generation in fml.coffee

    kppullin authored
    Swapped the usage of `Math.round` with `Math.floor` to prevent the generation of an invalid array index and subsequent exception.
    
    
    Example:
    
    Assume Math.random() returns `.9` and the array length is 5.
    
    `Math.round(.9 * 5)` yields 5, exceeding the bounds of the array, and triggers an exception.
    `Math.floor(.9 * 5)` yields 4, a valid index and the script works as expected.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  src/scripts/fml.coffee
View
2  src/scripts/fml.coffee
@@ -22,7 +22,7 @@ fml = (msg) ->
.http('http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/fmylife')
.get() (err, res, body) ->
fmls = JSON.parse(body)
- random = Math.round(Math.random() * fmls.responseData.feed.entries.length)
+ random = Math.floor(Math.random() * fmls.responseData.feed.entries.length)
text = fmls.responseData.feed.entries[random].content
text = text[0..text.indexOf('<img')-1]
msg.send text
Something went wrong with that request. Please try again.