title: Issue opening JPEG file with libjpeg: possible fopen failure?
body: <p>I try to open a jpeg file with jpeglib, and I get a runtime error in reading the jpeg file header. On further inspection I started suspecting that the FILE* after fopen is probably not valid.</p>
  
  <p>I am ripping out GDI+ from an older legacy Windows service that grabs images from a folder and prints them to a local printer. This service failed on Windows Server 2012, and it appears GDI+ is not supported inside Windows services:<br>
  <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms533798(v=VS.85).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/windows/desktop/ms533798(v=VS.85).aspx</a>
  "GDI+ functions and classes are not supported for use within a Windows service."</p>
  
  <p>I succeeded in replacing GDI+ calls with Win32 API print functions, and the function below attempts to open a JPEG image with jpeglib. </p>
  
  <pre><code>HBITMAP LoadJpegAsBitmap(LPCSTR pszFileName)
  { // Function copied fom http://katahiromz.web.fc2.com/win32/loadjpeg.html
  
      FILE *fp;
      struct jpeg_decompress_struct decomp;
      struct jpeg_error_mgr jerror;
      BITMAPINFO bi;
      LPBYTE lpBuf, pb;
      HBITMAP hbm;
      JSAMPARRAY buffer;
      INT row;
  
      fp = fopen(pszFileName, "rb");
      if (fp == NULL)  //&lt;- fp is not NULL, but suspect...
          return NULL;
  
      decomp.err = jpeg_std_error(&amp;jerror);
      jpeg_create_decompress(&amp;decomp);
      jpeg_stdio_src(&amp;decomp, fp);
      jpeg_read_header(&amp;decomp, TRUE); //&lt;- RUNTIME CRASH
      jpeg_start_decompress(&amp;decomp);
  
      //... ... ...
      //... ..
  }
  </code></pre>
  
  <p>After calling fopen at "if (fp == NULL)" the pointer has a value like 0x5fa97350 so the function does not return after the NULL check. 
  Visual Studio runtime inspection shows the FILE* member variables are null at that stage, like so:</p>
  
  <pre><code>fp-&gt;
          __ptr = 0x00000000 (NULL)
          _cnt = 0
          _base = 0x00000000 (NULL)
          _flag = 1
          _file = 3
          _charbuf = 0
          _bufsiz = 0
          _tmpfname = 0x00000000 (NULL)
  </code></pre>
  
  <p>The nulls led me to suspect the file did not open correctly. 
  To test, I inserted the code below just after the nullptr check:
  char c = getc(fp);</p>
  
  <pre><code>while (c != EOF) // file is EOF from the very first byte
  {                // loop is not executed
      putchar(c);
      c = getc(fp);
  }   
  </code></pre>
  
  <p>Above check convinced me the FILE* is not really valid after the fopen call.</p>
  
  <p>Anyway, the next steps in the function are where the jpeg image is opened.
  After calling jpeg_stdio_src(&amp;decomp, fp); the decomp structure shows 0xcccccccc for client_data. I suspect that explains why jpeglib crashes at the next line: a runtime exception inside jpeg_read_header(&amp;decomp, TRUE); (Can be explained if fp is corrupt). </p>
  
  <p>I do not think this is a duplicate of other stackoverflow questions regarding fopen:</p>
  
  <ul>
  <li>I do not get null after calling fopen // <a href="https://stackoverflow.com/questions/24081999/c-fopen-doesnt-work-returns-null-pointer">C - fopen() doesn&#39;t work (returns null pointer)</a></li>
  <li>The jpeg file has no security privileges, and I am admin on the machine anyway. <a href="https://stackoverflow.com/questions/19330987/simple-fopen-and-fprintf-not-working">Simple fopen and fprintf not working</a></li>
  <li>I hard-coded a full, valid path, so the path is not the issue. </li>
  <li>I supplied double backslashes for the Windows path. Forward slash made no difference</li>
  <li>I built the above with Unicode on and off. No difference. </li>
  <li>Corrupt JPEG file? It opens fine in other software. like Paint, Photoshop.</li>
  </ul>
  
  <p>So I am at a loss here, not sure why the file apparently fails to open and be read in jpeglib.  </p>
  
  <p>I also looked at 
  <a href="https://stackoverflow.com/questions/5616216/need-help-in-reading-jpeg-file-using-libjpeg">Need help in reading JPEG file using libjpeg</a> but I think my code is in line with the examples there.</p>
  
  <p>I appreciate your help.</p>