devongovett/pdfkit — Issue #62: Ambiguous code
Author: bjouhier
Created: 2012-02-28 15:38:21+00:00
State: open

FYI, I ran into a nasty bug in my fork. At line 22 of lib/image/jpeg.coffee, you have the following code:

``` coffeescript
data.pos += data.readUInt16()
```

The problem is that `data.readUInt16()` increments `data.pos` by 2. So, this code is ambiguous and the compiler can either treat it as:

``` coffeescript
delta = data.readUInt16()
data.pos += delta
```

or

``` coffeescript
start = data.pos
data.pos = start + data.readUInt16()
```

The first version increment `data.pos` by `delta + 2` (incorrect). The second one increments it by `delta` (correct).

I hit the problem because I streamlined the code (to replace all the sync calls by async ones) and I was getting different behaviors in callback mode (2nd interpretation - ok) and fibers mode (1st interpretation - broken). Took me a while to figure out what was going on because I was not suspecting that this could come from an ambiguous construct.

This line should be rewritten to eliminate the ambiguity.