Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

fopen_s on Windows doesn't allow for concurrent read-only access of files

Aperta
#914 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

What's happening:

On Windows, and for specific versions of MSVC, the following code is used to open files: (see lines 2290-2297 in tinyxml2.cpp):

#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
    
FILE* fp = 0;
    
const errno_t err = fopen_s( &fp, filepath, mode );
    if ( err ) {
        return 0;
    }
#else
    FILE* fp = fopen( filepath, mode );

It would seem that this code was added to avoid a warning using MSVC, as using fopen does indeed result in a warning on MSVC14, with the warning message suggesting to use fopen_s instead.

According to the Microsoft API entry for fopen_s, however, (see the remarks section in https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-s-wfopen-s?view=msvc-170), fopen_s does not open files while allowing for file sharing.

This means that a multithreaded or multiprocess program opening a file using tinyxml will occasionally fail to obtain file access if another thread or process currently has that file open, even in read-only mode.

To avoid this issue, and also compile without a warning, I have used Microsoft's recommended alternative for file sharing, _fsopen for the Windows only case (this requres including Share.h at the beginning of the file, using the same #define that enables this code to be ran):

 FILE* fp = 0;
 fp = _fsopen(filename, mode, _SH_DENYNO);
 if (fp)
     return fp;
  return 0;

The downside to this function is that it does not return a descriptive error code as fopen_s does, however the error code wasn't used outside of the scope of this function, so perhaps that is alright, perhaps not. I have not observed any issues while using this alternative, but of course I may have missed one or more.

Lingua principale
C++
Stelle
5.8k
Fork
2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leethomason/tinyxml2

Tutte le issue di leethomason/tinyxml2

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.