Repository: opencv
Issue: opencv2.4.x JpegDecoder::readData 'bool result = false' should be volatile modification
Labels: bug,auto-transferred,priority: normal,affected: 2.4,category: imgcodecs

Transferred from http://code.opencv.org/issues/3649

```
|| Shine Sun on 2014-04-15 02:10
|| Priority: Normal
|| Affected: 2.4.0 - 2.4.8
|| Category: highgui-images
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x64 / Linux
```
## opencv2.4.x JpegDecoder::readData 'bool result = false' should be volatile modification

```
opencv-2.4.8/modules/highgui/src/grfmt_jpeg.cpp:516
*bool result = false;*(bool  JpegDecoder::readData( Mat& img )
should be 
*volatile bool result = false;*

when opencv process some images which will cause "JpegDecoder::readData jpeg_finish_decompress" fail.
compiled in debug mode, readData returns true, but in some release compilation mode, readData returns false

like the program test.cpp:
using namespace std;
static jmp_buf buf;

void first(void) {
    longjmp(buf,1);
    printf("first\n");
}

int main() {
    bool result = false;
    if ( ! setjmp(buf) ) {
        result = true;
        first();
    }
    cout << result << endl;
    return 0;
}

g++ test.cpp
  it prints '1'
g++ test.cpp -O1
  it prints '0'
g++ test.cpp -O2
  it prints '0'

This is because the compiler to help us do the optimization, causes the value of result is modified.
So it must be with volatile, to prevent the value of the optimizing compiler.

When calling imdecode processing these pictures, will find that imdecode would fail, but imread was successful. As the above modified, return imdecode can, as we expected.

If you have any questions, you can contact me.
```
## History
##### Evgeny Talanin on 2014-04-16 07:39

```
Thanks for reporting.
You can propose this fix and a corresponding test as a pull request as described at http://code.opencv.org/projects/opencv/wiki/How_to_contribute page.
This ensures your changes to be tested using OpenCV's build system before affecting code in repository.
-   Assignee set to Shine Sun
-   Category set to highgui-images
```
##### Shine Sun on 2014-04-18 02:11

```
Hi  Evgeny:
   This is my first time using git,I find difficulty when submitted to the opencv code. When I use " git push" command, it returns erorr like this "fatal: unable to access 'https://github.com/Itseez/opencv.git/': The requested URL returned error: 403" . How to solve this problem.
```
##### Shine Sun on 2014-04-18 02:11

```
Hi Evgeny:
This is my first time using git,I find difficulty when submitted to the opencv code. When I use " git push" command, it returns erorr like this "fatal: unable to access 'https://github.com/Itseez/opencv.git/': The requested URL returned error: 403" . How to solve this problem.

Evgeny Talanin wrote:
> Thanks for reporting.
> You can propose this fix and a corresponding test as a pull request as described at http://code.opencv.org/projects/opencv/wiki/How_to_contribute page.
> This ensures your changes to be tested using OpenCV's build system before affecting code in repository.
```
##### Evgeny Talanin on 2014-04-18 10:00

```
Yes, that's a bit tricky at the first time.

That's an expected behavior: nobody has rights to push directly to OpenCV repository since only code tested by our build system should get there. It seems like you missed a concept of forking mentioned in 'The instruction in brief' section at http://code.opencv.org/projects/opencv/wiki/How_to_contribute.

The correct git workflow is:
1. Fork OpenCV repo under your account (see https://help.github.com/articles/fork-a-repo for details) and then only use your fork for implementing new features.
2. Push your changes to your OpenCV fork (not https://github.com/Itseez/opencv.git but https://github.com/YOUR_ACCOUNT_NAME/opencv.git)
3. Create pull request from your fork to OpenCV's repository (see https://help.github.com/articles/using-pull-requests for details).
4. Wait until pull request will be automatically tested and manually reviewed.
5. Enjoy your contribution to the open source computer vision library :)

If still having troubles, please just make a patch (or just a set of changed files) and attach here, we'll submit it to testing.
```
##### Shine Sun on 2014-04-22 01:49

```
Hi Evgeny:
　　Thank you for your answer, I tried, but without success, I think the patch to you to let you do a better patch addresses:.. Http://shinenuaa.oss-cn-hangzhou.aliyuncs.com/3649.patch　 Bug described in my above post. If you have any questions, please contact me, thank you!
　
Evgeny Talanin wrote:
> Yes, that's a bit tricky at the first time.
> 
> That's an expected behavior: nobody has rights to push directly to OpenCV repository since only code tested by our build system should get there. It seems like you missed a concept of forking mentioned in 'The instruction in brief' section at http://code.opencv.org/projects/opencv/wiki/How_to_contribute.
> 
> The correct git workflow is:
> 1. Fork OpenCV repo under your account (see https://help.github.com/articles/fork-a-repo for details) and then only use your fork for implementing new features.
> 2. Push your changes to your OpenCV fork (not https://github.com/Itseez/opencv.git but https://github.com/YOUR_ACCOUNT_NAME/opencv.git)
> 3. Create pull request from your fork to OpenCV's repository (see https://help.github.com/articles/using-pull-requests for details).
> 4. Wait until pull request will be automatically tested and manually reviewed.
> 5. Enjoy your contribution to the open source computer vision library :)
> 
> If still having troubles, please just make a patch (or just a set of changed files) and attach here, we'll submit it to testing.
```
##### Shine Sun on 2014-04-24 02:18

```
-   Assignee changed from Shine Sun to Evgeny Talanin
```
##### Dmitry Retinskiy on 2014-05-28 04:21

```
-   Affected version changed from 2.4.8 (latest release) to 2.4.0 -
    2.4.8
-   Priority changed from High to Normal
-   Assignee changed from Evgeny Talanin to Shine Sun
-   Status changed from New to Open
```
##### Radek Svoboda on 2014-05-28 08:37

```
The other thing is that the return values of all jpeg_*  function calls are ignored, so don't expect readData() to be safe in production software. I would recommend to also correct them at least for grfmt_jpeg.cpp in this Bugfix.
```
##### Shine Sun on 2014-06-05 11:56

```
thanks!

Radek Svoboda wrote:
> The other thing is that the return values of all jpeg_*  function calls are ignored, so don't expect readData() to be safe in production software. I would recommend to also correct them at least for grfmt_jpeg.cpp in this Bugfix.
```