zxing/zxing — pull request #129 by jameswald (2014-04-29 07:29:39+00:00)
Title: Thread-safe lazy initialization in GenericGF

This pull request is an attempt to fix the following exception which is reported multiple times per day from an Android app that I work on:

```
Caused by: java.lang.IllegalArgumentException: Divide by 0
at com.google.zxing.common.reedsolomon.GenericGFPoly.divide(SourceFile:205)
at com.google.zxing.common.reedsolomon.ReedSolomonEncoder.encode(SourceFile:65)
at com.google.zxing.qrcode.encoder.Encoder.generateECBytes(SourceFile:436)
at com.google.zxing.qrcode.encoder.Encoder.interleaveWithECBytes(SourceFile:391)
at com.google.zxing.qrcode.encoder.Encoder.encode(SourceFile:144)
at com.google.zxing.qrcode.encoder.Encoder.encode(SourceFile:76)
...
```

The `initialized` field was not thread-safe, which allowed multiple threads to fall into `initialize()`. This concurrent re-initialization would potentially allow mutations to `expTable` and `logTable` to be observed by other threads during encodings.

I spent a while reviewing the code and came to the conclusion that this is the only possible explanation for the exception. I cannot reproduce this crash reliably, so I haven't been able to verify that it addresses the root cause. Although I wasn't able to reproduce the divide by zero, I was able to generate corrupt `QRCode`s. That problem should also be addressed by this fix.