[segyio] 187/376: Ensure that files are always open in binary mode
Jørgen Kvalsvik
jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:30 UTC 2017
This is an automated email from the git hooks/post-receive script.
jokva-guest pushed a commit to branch debian
in repository segyio.
commit 19bb21b4ada113f5a7815aff9285dc349308287a
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date: Wed Feb 15 13:45:16 2017 +0100
Ensure that files are always open in binary mode
Previously this was enforced by the python C bindings, but this is
common functionality to ensure correctness on multiple platforms, so it
makes sense to have it in the shared C core.
---
lib/src/segy.c | 20 ++++++++++++++++++--
python/segyio/_segyio.c | 19 ++++---------------
2 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/lib/src/segy.c b/lib/src/segy.c
index 11cf760..165f8fe 100644
--- a/lib/src/segy.c
+++ b/lib/src/segy.c
@@ -344,7 +344,23 @@ struct segy_file_handle {
};
segy_file* segy_open( const char* path, const char* mode ) {
- FILE* fp = fopen( path, mode );
+
+ if( !path || !mode ) return NULL;
+
+ // append a 'b' if it is not passed by the user; not a problem on unix, but
+ // windows and other platforms fail without it
+ char binary_mode[ 4 ] = { 0 };
+ strncpy( binary_mode, mode, 3 );
+
+ size_t mode_len = strlen( binary_mode );
+ if( binary_mode[ mode_len - 1 ] != 'b' ) binary_mode[ mode_len ] = 'b';
+
+ // Account for invalid mode. On unix this is fine, but windows crashes the
+ // process if mode is invalid
+ if( !strstr( "rb" "wb" "ab" "r+b" "w+b" "a+b", binary_mode ) )
+ return NULL;
+
+ FILE* fp = fopen( path, binary_mode );
if( !fp ) return NULL;
@@ -356,7 +372,7 @@ segy_file* segy_open( const char* path, const char* mode ) {
}
file->fp = fp;
- strncpy( file->mode, mode, 3 );
+ strcpy( file->mode, binary_mode );
return file;
}
diff --git a/python/segyio/_segyio.c b/python/segyio/_segyio.c
index d7a72b4..75c2c74 100644
--- a/python/segyio/_segyio.c
+++ b/python/segyio/_segyio.c
@@ -72,24 +72,13 @@ static PyObject *py_FILE_open(PyObject *self, PyObject *args) {
return NULL;
}
- // append a 'b' if it is not passed by the user; not a problem on unix, but
- // windows and other platforms fail without it
- // the heap alloc is expensive, but avoids the risk of local stack
- // smashing and is C++ compatible
- char* binary_mode = strcpy( calloc( mode_len + 2, sizeof( char ) ), mode );
- if( binary_mode[ mode_len - 1 ] != 'b' ) binary_mode[ mode_len ] = 'b';
-
- // Account for invalid mode. On unix this is fine, but windows crashes the
- // process if mode is invalid
- if( !strstr( "rb" "wb" "ab" "r+b" "w+b" "a+b", binary_mode ) ) {
- PyErr_Format( PyExc_IOError, "Invalid mode string '%s'", binary_mode );
- free( binary_mode );
+ segy_file *p_FILE = segy_open( filename, mode );
+
+ if( !p_FILE && !strstr( "rb" "wb" "ab" "r+b" "w+b" "a+b", mode ) ) {
+ PyErr_Format( PyExc_IOError, "Invalid mode string '%s'", mode );
return NULL;
}
- segy_file *p_FILE = segy_open( filename, binary_mode );
- free( binary_mode );
-
if (p_FILE == NULL) {
return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git
More information about the debian-science-commits
mailing list