[segyio] 183/376: Use signed integer for trace binary size
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 c97e464c66a0da880979a186b77cf134977f7770
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date: Fri Feb 10 10:51:06 2017 +0100
Use signed integer for trace binary size
Checking for >0 or trapping on overflow is more reasonable (since
arithmetic is performed), and ints carry enough information to
fully represent the size, so int is a better choice than size_t.
---
applications/segyinfo.c | 2 +-
applications/segyinspect.c | 2 +-
lib/include/segyio/segy.h | 34 +++++++++++++----------
lib/src/segy.c | 69 +++++++++++++++++++++++++++-------------------
lib/src/segyio/util.h | 2 +-
lib/test/segy.c | 16 +++++------
python/segyio/_segyio.c | 32 ++++++++++-----------
7 files changed, 87 insertions(+), 70 deletions(-)
diff --git a/applications/segyinfo.c b/applications/segyinfo.c
index edf9db0..c6cd1c2 100644
--- a/applications/segyinfo.c
+++ b/applications/segyinfo.c
@@ -51,7 +51,7 @@ int main(int argc, char* argv[]) {
const int format = segy_format( header );
const int samples = segy_samples( header );
const long trace0 = segy_trace0( header );
- const unsigned int trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
int extended_headers;
err = segy_get_bfield( header, SEGY_BIN_EXT_HEADERS, &extended_headers );
diff --git a/applications/segyinspect.c b/applications/segyinspect.c
index 643f77c..9c73f94 100644
--- a/applications/segyinspect.c
+++ b/applications/segyinspect.c
@@ -77,7 +77,7 @@ int main(int argc, char* argv[]) {
const int format = segy_format( header );
const int samples = segy_samples( header );
const long trace0 = segy_trace0( header );
- const unsigned int trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
size_t traces;
err = segy_traces( fp, &traces, trace0, trace_bsize );
diff --git a/lib/include/segyio/segy.h b/lib/include/segyio/segy.h
index d5d72be..f4feda5 100644
--- a/lib/include/segyio/segy.h
+++ b/lib/include/segyio/segy.h
@@ -61,11 +61,15 @@ int segy_get_bfield( const char* binheader, int field, int32_t* f );
int segy_set_field( char* traceheader, int field, int32_t val );
int segy_set_bfield( char* binheader, int field, int32_t val );
-unsigned segy_trace_bsize( int samples );
+/*
+ * exception: segy_trace_bsize computes the size of the traces in bytes. Cannot
+ * fail.
+ */
+int segy_trace_bsize( int samples );
/* byte-offset of the first trace header. */
long segy_trace0( const char* binheader );
/* number of traces in this file */
-int segy_traces( segy_file*, size_t*, long trace0, unsigned int trace_bsize );
+int segy_traces( segy_file*, size_t*, long trace0, int trace_bsize );
int segy_sample_indexes(segy_file* fp, double* buf, double t0, double dt, size_t count);
@@ -79,14 +83,14 @@ int segy_traceheader( segy_file*,
unsigned int traceno,
char* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/* Read the trace header at `traceno` into `buf`. */
int segy_write_traceheader( segy_file*,
unsigned int traceno,
const char* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Number of traces in this file. The sorting type will be written to `sorting`
@@ -98,7 +102,7 @@ int segy_sorting( segy_file*,
int xl,
int* sorting,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Number of offsets in this file, written to `offsets`. 1 if a 3D data set, >1
@@ -110,7 +114,7 @@ int segy_offsets( segy_file*,
unsigned int traces,
unsigned int* offsets,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* The names of the individual offsets. `out` must be a buffer of
@@ -121,7 +125,7 @@ int segy_offset_indices( segy_file*,
int offsets,
int* out,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* read/write traces. Does not manipulate the buffers at all, i.e. in order to
@@ -132,13 +136,13 @@ int segy_readtrace( segy_file*,
unsigned int traceno,
float* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
int segy_writetrace( segy_file*,
unsigned int traceno,
const float* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/* convert to/from native float from segy formats (likely IBM or IEEE) */
int segy_to_native( int format,
@@ -156,7 +160,7 @@ int segy_read_line( segy_file* fp,
int offsets,
float* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
int segy_write_line( segy_file* fp,
unsigned int line_trace0,
@@ -165,7 +169,7 @@ int segy_write_line( segy_file* fp,
int offsets,
const float* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Count inlines and crosslines. Use this function to determine how large buffer
@@ -185,7 +189,7 @@ int segy_count_lines( segy_file*,
unsigned int* l1out,
unsigned int* l2out,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Alternative interface for segy_count_lines. If you have information about
@@ -201,7 +205,7 @@ int segy_lines_count( segy_file*,
int* il_count,
int* xl_count,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Find the `line_length` for the inlines. Assumes all inlines, crosslines and
* traces don't vary in length.
@@ -225,7 +229,7 @@ int segy_inline_indices( segy_file*,
unsigned int offsets,
unsigned int* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
int segy_crossline_indices( segy_file*,
int xl,
@@ -235,7 +239,7 @@ int segy_crossline_indices( segy_file*,
unsigned int offsets,
unsigned int* buf,
long trace0,
- unsigned int trace_bsize );
+ int trace_bsize );
/*
* Find the first `traceno` of the line `lineno`. `linenos` should be the line
diff --git a/lib/src/segy.c b/lib/src/segy.c
index 698480b..260803e 100644
--- a/lib/src/segy.c
+++ b/lib/src/segy.c
@@ -557,7 +557,8 @@ int segy_samples( const char* buf ) {
return samples;
}
-unsigned int segy_trace_bsize( int samples ) {
+int segy_trace_bsize( int samples ) {
+ assert( samples >= 0 );
/* Hard four-byte float assumption */
return samples * 4;
}
@@ -573,7 +574,7 @@ long segy_trace0( const char* binheader ) {
int segy_seek( segy_file* fp,
int trace,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
trace_bsize += SEGY_TRACE_HEADER_SIZE;
long long pos = (long long)trace0 + (trace * (long long)trace_bsize);
@@ -613,7 +614,7 @@ int segy_traceheader( segy_file* fp,
unsigned int traceno,
char* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
const int err = segy_seek( fp, traceno, trace0, trace_bsize );
if( err != 0 ) return err;
@@ -635,7 +636,7 @@ int segy_write_traceheader( segy_file* fp,
unsigned int traceno,
const char* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
const int err = segy_seek( fp, traceno, trace0, trace_bsize );
if( err != 0 ) return err;
@@ -662,14 +663,16 @@ int segy_write_traceheader( segy_file* fp,
int segy_traces( segy_file* fp,
size_t* traces,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
long long fsize;
int err = file_size( fp->fp, &fsize );
if( err != 0 ) return err;
+ if( trace0 > fsize ) return SEGY_INVALID_ARGS;
+
trace_bsize += SEGY_TRACE_HEADER_SIZE;
- const size_t trace_data_size = fsize - trace0;
+ const int trace_data_size = fsize - trace0;
if( trace_data_size % trace_bsize != 0 )
return SEGY_TRACE_SIZE_MISMATCH;
@@ -690,7 +693,7 @@ int segy_sample_interval( segy_file* fp, double* dt) {
const long trace0 = segy_trace0( bin_header );
int samples = segy_samples( bin_header );
- const size_t trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
err = segy_traceheader(fp, 0, trace_header, trace0, trace_bsize);
if (err != 0) {
@@ -752,7 +755,7 @@ int segy_sorting( segy_file* fp,
int xl,
int* sorting,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
char traceheader[ SEGY_TRACE_HEADER_SIZE ];
@@ -824,7 +827,7 @@ int segy_offsets( segy_file* fp,
unsigned int traces,
unsigned int* out,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
int il0, il1, xl0, xl1;
char header[ SEGY_TRACE_HEADER_SIZE ];
@@ -867,7 +870,7 @@ int segy_offset_indices( segy_file* fp,
int offsets,
int* out,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err = 0;
int32_t x = 0;
char header[ SEGY_TRACE_HEADER_SIZE ];
@@ -893,7 +896,7 @@ static int segy_line_indices( segy_file* fp,
unsigned int num_indices,
unsigned int* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
if( field_size[ field ] == 0 )
return SEGY_INVALID_FIELD;
@@ -915,7 +918,7 @@ static int count_lines( segy_file* fp,
unsigned int offsets,
unsigned int* out,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
char header[ SEGY_TRACE_HEADER_SIZE ];
@@ -956,7 +959,7 @@ int segy_count_lines( segy_file* fp,
unsigned int* l1out,
unsigned int* l2out,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
unsigned int l2count;
@@ -984,7 +987,7 @@ int segy_lines_count( segy_file* fp,
int* il_count,
int* xl_count,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
if( sorting == SEGY_UNKNOWN_SORTING ) return SEGY_INVALID_SORTING;
@@ -1027,7 +1030,7 @@ int segy_inline_indices( segy_file* fp,
unsigned int offsets,
unsigned int* buf,
long trace0,
- unsigned int trace_bsize) {
+ int trace_bsize) {
int err;
if( sorting == SEGY_INLINE_SORTING ) {
@@ -1054,7 +1057,7 @@ int segy_crossline_indices( segy_file* fp,
unsigned int offsets,
unsigned int* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
@@ -1089,7 +1092,7 @@ int segy_readtrace( segy_file* fp,
unsigned int traceno,
float* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
err = segy_seek( fp, traceno, trace0, trace_bsize );
if( err != 0 ) return err;
@@ -1097,13 +1100,16 @@ int segy_readtrace( segy_file* fp,
err = skip_traceheader( fp );
if( err != 0 ) return err;
+ assert( trace_bsize >= 0 );
+ const size_t bsize = (size_t) trace_bsize;
+
if( fp->addr ) {
- memcpy( buf, fp->cur, trace_bsize );
+ memcpy( buf, fp->cur, bsize );
return SEGY_OK;
}
- const size_t readc = fread( buf, 1, trace_bsize, fp->fp );
- if( readc != trace_bsize ) return SEGY_FREAD_ERROR;
+ const size_t readc = fread( buf, 1, bsize, fp->fp );
+ if( readc != bsize ) return SEGY_FREAD_ERROR;
return SEGY_OK;
@@ -1113,7 +1119,7 @@ int segy_writetrace( segy_file* fp,
unsigned int traceno,
const float* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
int err;
err = segy_seek( fp, traceno, trace0, trace_bsize );
@@ -1122,13 +1128,16 @@ int segy_writetrace( segy_file* fp,
err = skip_traceheader( fp );
if( err != 0 ) return err;
+ assert( trace_bsize >= 0 );
+ const size_t bsize = (size_t) trace_bsize;
+
if( fp->addr ) {
- memcpy( fp->cur, buf, trace_bsize );
+ memcpy( fp->cur, buf, bsize );
return SEGY_OK;
}
- const size_t writec = fwrite( buf, 1, trace_bsize, fp->fp );
- if( writec != trace_bsize )
+ const size_t writec = fwrite( buf, 1, bsize , fp->fp );
+ if( writec != bsize )
return SEGY_FWRITE_ERROR;
return SEGY_OK;
}
@@ -1215,9 +1224,11 @@ int segy_read_line( segy_file* fp,
int offsets,
float* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
- const size_t trace_data_size = trace_bsize / 4;
+ assert( sizeof( float ) == sizeof( int32_t ) );
+ assert( trace_bsize % 4 == 0 );
+ const int trace_data_size = trace_bsize / 4;
stride *= offsets;
@@ -1247,9 +1258,11 @@ int segy_write_line( segy_file* fp,
int offsets,
const float* buf,
long trace0,
- unsigned int trace_bsize ) {
+ int trace_bsize ) {
- const size_t trace_data_size = trace_bsize / 4;
+ assert( sizeof( float ) == sizeof( int32_t ) );
+ assert( trace_bsize % 4 == 0 );
+ const int trace_data_size = trace_bsize / 4;
line_trace0 *= offsets;
stride *= offsets;
diff --git a/lib/src/segyio/util.h b/lib/src/segyio/util.h
index a2494c3..b461779 100644
--- a/lib/src/segyio/util.h
+++ b/lib/src/segyio/util.h
@@ -18,7 +18,7 @@ void ebcdic2ascii( const char* ebcdic, char* ascii );
void ascii2ebcdic( const char* ascii, char* ebcdic );
void ibm2ieee(void* to, const void* from);
void ieee2ibm(void* to, const void* from);
-int segy_seek( struct segy_file_handle*, int, long, unsigned int );
+int segy_seek( struct segy_file_handle*, unsigned int, long, int );
long long segy_ftell( struct segy_file_handle* );
#ifdef __cplusplus
diff --git a/lib/test/segy.c b/lib/test/segy.c
index 311681f..f313e74 100644
--- a/lib/test/segy.c
+++ b/lib/test/segy.c
@@ -37,7 +37,7 @@ static void test_interpret_file() {
const int samples = segy_samples( header );
assertTrue( samples == 50, "Expected 350 samples per trace." );
- const size_t trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
assertTrue( trace_bsize == 50 * 4,
"Wrong trace byte size. Expected samples * 4-byte float." );
@@ -222,7 +222,7 @@ static void testReadInLine_4(){
assertTrue( 0 == segy_binheader( fp, header ), "Could not read header" );
const long trace0 = segy_trace0( header );
const int samples = segy_samples( header );
- const size_t trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
const int format = segy_format( header );
unsigned int inline_indices[ 5 ];
@@ -297,7 +297,7 @@ static void testReadCrossLine_22(){
assertTrue( 0 == segy_binheader( fp, header ), "Could not read header" );
const long trace0 = segy_trace0( header );
const int samples = segy_samples( header );
- const size_t trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
const int format = segy_format( header );
unsigned int crossline_indices[ 5 ];
@@ -367,7 +367,7 @@ static void test_modify_trace_header() {
assertTrue( err == 0, "Could not read header" );
const long trace0 = segy_trace0( bheader );
const int samples = segy_samples( bheader );
- const size_t trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
char traceh[ SEGY_TRACE_HEADER_SIZE ];
err = segy_traceheader( fp, 0, traceh, trace0, trace_bsize );
@@ -471,7 +471,7 @@ static void test_trace_header_errors() {
err = segy_binheader( fp, binheader );
assertTrue( err == 0, "Could not read binary header." );
const int samples = segy_samples( binheader );
- const unsigned bsize = segy_trace_bsize( samples );
+ const int bsize = segy_trace_bsize( samples );
const long trace0 = segy_trace0( binheader );
char header[ SEGY_TRACE_HEADER_SIZE ];
@@ -509,7 +509,7 @@ static void test_file_error_codes() {
"Could read binary header from invalid file." );
const int samples = segy_samples( binheader );
- const unsigned trace_bsize = segy_trace_bsize( samples );
+ const int trace_bsize = segy_trace_bsize( samples );
const long trace0 = segy_trace0( binheader );
char header[ SEGY_TRACE_HEADER_SIZE ];
@@ -575,8 +575,8 @@ static void test_error_codes_sans_file() {
static void test_file_size_above_4GB(){
segy_file* fp = segy_open( "4gbfile", "w+b" );
- unsigned int trace = 5000000;
- unsigned int trace_bsize = 1000;
+ int trace = 5000000;
+ int trace_bsize = 1000;
long long tracesize = trace_bsize + SEGY_TRACE_HEADER_SIZE;
long trace0 = 0;
diff --git a/python/segyio/_segyio.c b/python/segyio/_segyio.c
index 0a51442..66bc602 100644
--- a/python/segyio/_segyio.c
+++ b/python/segyio/_segyio.c
@@ -442,9 +442,9 @@ static PyObject *py_read_trace_header(PyObject *self, PyObject *args) {
unsigned int traceno;
PyObject *trace_header_capsule = NULL;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
- PyArg_ParseTuple(args, "OIOlI", &file_capsule, &traceno, &trace_header_capsule, &trace0, &trace_bsize);
+ PyArg_ParseTuple(args, "OIOli", &file_capsule, &traceno, &trace_header_capsule, &trace0, &trace_bsize);
segy_file *p_FILE = get_FILE_pointer_from_capsule(file_capsule);
@@ -470,9 +470,9 @@ static PyObject *py_write_trace_header(PyObject *self, PyObject *args) {
unsigned int traceno;
PyObject *trace_header_capsule = NULL;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
- PyArg_ParseTuple(args, "OIOlI", &file_capsule, &traceno, &trace_header_capsule, &trace0, &trace_bsize);
+ PyArg_ParseTuple(args, "OIOli", &file_capsule, &traceno, &trace_header_capsule, &trace0, &trace_bsize);
segy_file *p_FILE = get_FILE_pointer_from_capsule(file_capsule);
@@ -497,9 +497,9 @@ static PyObject *py_trace_bsize(PyObject *self, PyObject *args) {
PyArg_ParseTuple(args, "i", &sample_count);
- unsigned int byte_count = segy_trace_bsize(sample_count);
+ int byte_count = segy_trace_bsize(sample_count);
- return Py_BuildValue("I", byte_count);
+ return Py_BuildValue("i", byte_count);
}
static PyObject *py_get_dt(PyObject *self, PyObject *args) {
@@ -571,7 +571,7 @@ static PyObject *py_init_metrics(PyObject *self, PyObject *args) {
long trace0 = segy_trace0(binary_header);
int sample_count = segy_samples(binary_header);
int format = segy_format(binary_header);
- unsigned int trace_bsize = segy_trace_bsize(sample_count);
+ int trace_bsize = segy_trace_bsize(sample_count);
int sorting;
int error = segy_sorting(p_FILE, il_field, xl_field, &sorting, trace0, trace_bsize);
@@ -629,7 +629,7 @@ static PyObject *py_init_metrics(PyObject *self, PyObject *args) {
PyDict_SetItemString(dict, "trace0", Py_BuildValue("l", trace0));
PyDict_SetItemString(dict, "sample_count", Py_BuildValue("i", sample_count));
PyDict_SetItemString(dict, "format", Py_BuildValue("i", format));
- PyDict_SetItemString(dict, "trace_bsize", Py_BuildValue("I", trace_bsize));
+ PyDict_SetItemString(dict, "trace_bsize", Py_BuildValue("i", trace_bsize));
PyDict_SetItemString(dict, "sorting", Py_BuildValue("i", sorting));
PyDict_SetItemString(dict, "trace_count", Py_BuildValue("k", trace_count));
PyDict_SetItemString(dict, "offset_count", Py_BuildValue("I", offset_count));
@@ -718,14 +718,14 @@ static PyObject *py_init_indices(PyObject *self, PyObject *args) {
int offset_field;
int sorting;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
PyArg_Parse(PyDict_GetItemString(metrics, "iline_field"), "i", &il_field);
PyArg_Parse(PyDict_GetItemString(metrics, "xline_field"), "i", &xl_field);
PyArg_Parse(PyDict_GetItemString(metrics, "offset_field"), "i", &offset_field);
PyArg_Parse(PyDict_GetItemString(metrics, "sorting"), "i", &sorting);
PyArg_Parse(PyDict_GetItemString(metrics, "trace0"), "l", &trace0);
- PyArg_Parse(PyDict_GetItemString(metrics, "trace_bsize"), "I", &trace_bsize);
+ PyArg_Parse(PyDict_GetItemString(metrics, "trace_bsize"), "i", &trace_bsize);
int error = segy_inline_indices(p_FILE, il_field, sorting, iline_count, xline_count, offset_count, iline_buffer.buf,
trace0, trace_bsize);
@@ -793,7 +793,7 @@ static PyObject *py_read_trace(PyObject *self, PyObject *args) {
PyObject *buffer_out;
int trace_count;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
int format;
unsigned int samples;
@@ -868,7 +868,7 @@ static PyObject *py_write_trace(PyObject *self, PyObject *args) {
unsigned int trace_no;
PyObject *buffer_in;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
int format;
unsigned int samples;
@@ -917,7 +917,7 @@ static PyObject *py_read_line(PyObject *self, PyObject *args) {
int offsets;
PyObject *buffer_in;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
int format;
unsigned int samples;
@@ -966,11 +966,11 @@ static PyObject *py_read_depth_slice(PyObject *self, PyObject *args) {
int offsets;
PyObject *buffer_out;
long trace0;
- unsigned int trace_bsize;
+ int trace_bsize;
int format;
- unsigned int samples;
+ int samples;
- PyArg_ParseTuple(args, "OiiiOlIiI", &file_capsule,
+ PyArg_ParseTuple(args, "OiiiOliii", &file_capsule,
&depth,
&count,
&offsets,
--
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