title: Assertion error in CRT calling _osfile() in VS 2008?
body: <p>I have a C++ code base that has been working for a long time.  The code base was a legacy VS 2003 set of projects that I recently migrated to VS 2008.  The migration seemed to be successful in that the resulting program built, and run.</p>
  
  <p>I reinstalled my OS and all applications on a fresh drive, and now when I attempt to debug the program within the debugger, I receive an assertion error inside the CRT's <code>chsize</code> (really, <code>_chsize_s</code>).  Specifically (cropped to essentials, ignoring safety checks):</p>
  
  <pre><code>FILE * testfile = fopen("P:\\_Dan\\local\\foogoo.txt", "w");
  int filehandle = fileno(testfile);
  chsize(filehandle, 0);
  fwrite("goohoo", 1, 6, testfile);
  fclose(testfile);
  </code></pre>
  
  <p>The debug assertion occurs within <code>chsize</code> - specifically, within the CRT's source code file chsize.c, at the following line:</p>
  
  <pre><code> _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE((_osfile(filedes) &amp; FOPEN), EBADF);
  </code></pre>
  
  <p>... where <code>filedes</code> matches <code>filehandle</code>.</p>
  
  <p>I thought possibly the problem might result from not having an older version of VS installed on the new system (only VS 2008), because some 3rd-party libraries require VS 8.0 redistributable - even though on the old system things seemed to be building and running just fine using VS 2008.  I therefore installed VS 2005 (not 2003).  However, the problem continues to occur.</p>
  
  <p>Any suggestions would be immensely welcome.</p>
  
  <p><em>*</em> Update - The issue is unrelated to <code>chsize</code>.  See my answer below.</p>