fopen_s on Windows doesn't allow for concurrent read-only access of files
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leethomason/tinyxml2
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
leethomason/tinyxml2#1097 ·
-
New release requestAperta
leethomason/tinyxml2#1087 · 4 commenti ·
-
leethomason/tinyxml2#1085 · 1 commento ·
-
New relase requestAperta
leethomason/tinyxml2#1084 ·
-
leethomason/tinyxml2#1083 ·
Tutte le issue di leethomason/tinyxml2
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
? - Needs Triage bot_watch bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
NVIDIA/cudf-spark-jni#5267 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
(S1 - Need confirmation)
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
CleverRaven/Cataclysm-DDA#88974 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
microsoft/onnxruntime#33215 ·
I maintainer di solito rispondono entro 2 giorni