From 2ea07131e5e39aa734a2c263cca34dd5fd741db2 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 3 Oct 2023 17:52:13 +0100 Subject: [PATCH] fix MSVC warnings, version bump (#69) fix MSVC warnings, version bump --- CHANGELOG.md | 9 +++++++++ meson.build | 2 +- src/dicom-data.c | 24 ++++++++++++------------ src/dicom-file.c | 22 +++++++++++----------- src/dicom-io.c | 23 ++++++++++++----------- tests/check_dicom.c | 27 ++++++++++++++------------- 6 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..286531f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +## master + +- fix MSVC compiler warnings [jcupitt] +- only use `__attribute__((constructor))` if supported [bgilbert] + +## 1.0.0, 2/9/23 + +- first release! + diff --git a/meson.build b/meson.build index 3b595b1..0d0b6f4 100644 --- a/meson.build +++ b/meson.build @@ -8,7 +8,7 @@ project( ], license : 'MIT', meson_version : '>=0.50', - version : '1.0.0' + version : '1.0.1' ) if not meson.is_subproject() meson.add_dist_script( diff --git a/src/dicom-data.c b/src/dicom-data.c index 1f86296..9147d26 100644 --- a/src/dicom-data.c +++ b/src/dicom-data.c @@ -478,9 +478,9 @@ bool dcm_element_set_value_string_multi(DcmError **error, element->vm = vm; - size_t length = 0; + uint32_t length = 0; for (uint32_t i = 0; i < vm; i++) { - length += strlen(values[i]); + length += (uint32_t) strlen(values[i]); } if (vm > 1) { // add the separator characters @@ -576,7 +576,7 @@ bool dcm_element_set_value_string(DcmError **error, } element->vm = 1; - element_set_length(element, strlen(value)); + element_set_length(element, (uint32_t) strlen(value)); if (!dcm_element_validate(error, element)) { return false; @@ -598,7 +598,7 @@ static int64_t value_to_int64(DcmVR vr, int *value) { uint64_t result = 0; -#define PEEK(TYPE) result = *((TYPE *) value) +#define PEEK(TYPE) result = (uint64_t) *((TYPE *) value) DCM_SWITCH_NUMERIC(vr, PEEK); #undef PEEK @@ -610,7 +610,7 @@ static int64_t value_to_int64(DcmVR vr, int *value) static void int64_to_value(DcmVR vr, int *result, int64_t value) { *result = 0; -#define POKE(TYPE) *((TYPE *) result) = value; +#define POKE(TYPE) *((TYPE *) result) = (TYPE) value; DCM_SWITCH_NUMERIC(vr, POKE); #undef POKE } @@ -697,7 +697,7 @@ bool dcm_element_set_value_integer(DcmError **error, int *element_value = (int *) &element->value.single.sl; int64_to_value(element->vr, element_value, value); element->vm = 1; - element_set_length(element, dcm_dict_vr_size(element->vr)); + element_set_length(element, (uint32_t) dcm_dict_vr_size(element->vr)); if (!dcm_element_validate(error, element)) { return false; @@ -741,7 +741,7 @@ bool dcm_element_set_value_numeric_multi(DcmError **error, } element->vm = vm; - element_set_length(element, size_in_bytes); + element_set_length(element, (uint32_t) size_in_bytes); if (!dcm_element_validate(error, element)) { return false; @@ -762,7 +762,7 @@ static double value_to_double(DcmVR vr, double *value) { double result = 0.0; -#define PEEK(TYPE) result = *((TYPE *) value) +#define PEEK(TYPE) result = (double) *((TYPE *) value) DCM_SWITCH_NUMERIC(vr, PEEK); #undef PEEK @@ -774,7 +774,7 @@ static double value_to_double(DcmVR vr, double *value) static void double_to_value(DcmVR vr, double *result, double value) { *result = 0.0; -#define POKE(TYPE) *((TYPE *) result) = value; +#define POKE(TYPE) *((TYPE *) result) = (TYPE) value; DCM_SWITCH_NUMERIC(vr, POKE); #undef POKE } @@ -834,7 +834,7 @@ bool dcm_element_set_value_decimal(DcmError **error, double *element_value = (double *) &element->value.single.fd; double_to_value(element->vr, element_value, value); element->vm = 1; - element_set_length(element, dcm_dict_vr_size(element->vr)); + element_set_length(element, (uint32_t) dcm_dict_vr_size(element->vr)); if (!dcm_element_validate(error, element)) { return false; @@ -948,7 +948,7 @@ bool dcm_element_set_value(DcmError **error, if (!dcm_element_set_value_numeric_multi(error, element, (int *) value, - length / size, + length / (uint32_t) size, steal)) { return false; } @@ -1284,7 +1284,7 @@ char *dcm_element_value_to_string(const DcmElement *element) (void) dcm_element_get_value_integer(NULL, element, 0, &grp); (void) dcm_element_get_value_integer(NULL, element, 1, &ele); - uint32_t tag = grp << 16 | ele; + uint32_t tag = (uint32_t) grp << 16 | (uint32_t) ele; const char *keyword = dcm_dict_keyword_from_tag(tag); diff --git a/src/dicom-file.c b/src/dicom-file.c index f1f05b7..e277f29 100644 --- a/src/dicom-file.c +++ b/src/dicom-file.c @@ -319,8 +319,8 @@ static bool get_frame_size(DcmError **error, return false; } - *frame_width = width; - *frame_height = height; + *frame_width = (uint32_t) width; + *frame_height = (uint32_t) height; return true; } @@ -350,8 +350,8 @@ static bool get_tiles(DcmError **error, height = frame_width; (void) get_tag_int(NULL, metadata, "TotalPixelMatrixRows", &height); - *tiles_across = width / frame_width + !!(width % frame_width); - *tiles_down = height / frame_height + !!(height % frame_height); + *tiles_across = (uint32_t) width / frame_width + !!(width % frame_width); + *tiles_down = (uint32_t) height / frame_height + !!(height % frame_height); return true; } @@ -664,49 +664,49 @@ static bool set_pixel_description(DcmError **error, !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->rows = value; + desc->rows = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280011); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->columns = value; + desc->columns = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280002); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->samples_per_pixel = value; + desc->samples_per_pixel = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280100); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->bits_allocated = value; + desc->bits_allocated = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280101); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->bits_stored = value; + desc->bits_stored = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280103); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->pixel_representation = value; + desc->pixel_representation = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280006); if (element == NULL || !dcm_element_get_value_integer(error, element, 0, &value)) { return false; } - desc->planar_configuration = value; + desc->planar_configuration = (uint16_t) value; element = dcm_dataset_get(error, metadata, 0x00280004); if (element == NULL || diff --git a/src/dicom-io.c b/src/dicom-io.c index 0ac70e0..b4a05ca 100644 --- a/src/dicom-io.c +++ b/src/dicom-io.c @@ -44,8 +44,8 @@ typedef struct _DcmIOFile { int fd; char *filename; char input_buffer[BUFFER_SIZE]; - int bytes_in_buffer; - int read_point; + int64_t bytes_in_buffer; + int64_t read_point; } DcmIOFile; @@ -120,7 +120,8 @@ static int64_t read_file(DcmError **error, DcmIOFile *file, int64_t bytes_read; #ifdef _WIN32 - bytes_read = _read(file->fd, buffer, length); + // we'll never read a huge chunk (hopefully) + bytes_read = _read(file->fd, buffer, (uint32_t) length); #else do { bytes_read = read(file->fd, buffer, length); @@ -179,8 +180,8 @@ static int64_t dcm_io_read_file(DcmError **error, DcmIO *io, /* Read what we can from the buffer. */ - int bytes_available = file->bytes_in_buffer - file->read_point; - int bytes_to_copy = MIN(bytes_available, length); + int64_t bytes_available = file->bytes_in_buffer - file->read_point; + int64_t bytes_to_copy = MIN(bytes_available, length); memcpy(buffer, file->input_buffer + file->read_point, @@ -309,8 +310,8 @@ static int64_t dcm_io_read_memory(DcmError **error, DcmIO *io, USED(error); - int bytes_available = memory->length - memory->read_point; - int bytes_to_copy = MIN(bytes_available, length); + int64_t bytes_available = memory->length - memory->read_point; + int64_t bytes_to_copy = MIN(bytes_available, length); memcpy(buffer, memory->buffer + memory->read_point, bytes_to_copy); @@ -355,7 +356,7 @@ static int64_t dcm_io_seek_memory(DcmError **error, DcmIO *io, DcmIO *dcm_io_create_from_memory(DcmError **error, - const char *buffer, + const char *buffer, int64_t length) { static DcmIOMethods methods = { @@ -391,9 +392,9 @@ int64_t dcm_io_read(DcmError **error, } -int64_t dcm_io_seek(DcmError **error, - DcmIO *io, - int64_t offset, +int64_t dcm_io_seek(DcmError **error, + DcmIO *io, + int64_t offset, int whence) { return io->methods->seek(error, io, offset, whence); diff --git a/tests/check_dicom.c b/tests/check_dicom.c index 87cd22b..afe4081 100644 --- a/tests/check_dicom.c +++ b/tests/check_dicom.c @@ -70,7 +70,7 @@ static size_t compute_length_of_string_value_multi(char **values, } -static char *load_file_to_memory(const char *name, long *length_out) +static char *load_file_to_memory(const char *name, int64_t *length_out) { FILE *fp; @@ -82,7 +82,7 @@ static char *load_file_to_memory(const char *name, long *length_out) free(full_path); fseek(fp, 0, SEEK_END); - long length = ftell(fp); + int64_t length = ftell(fp); fseek(fp, 0, SEEK_SET); if (length < 0) { @@ -96,12 +96,12 @@ static char *load_file_to_memory(const char *name, long *length_out) return NULL; } - long total_read = 0; + int64_t total_read = 0; while (total_read < length) { - size_t bytes_read = fread(result + total_read, - 1, - length - total_read, - fp); + int64_t bytes_read = fread(result + total_read, + 1, + length - total_read, + fp); total_read += bytes_read; } @@ -302,7 +302,8 @@ START_TEST(test_element_CS_multivalue_empty) uint32_t tag = 0x00080008; uint32_t vm = 0; - char **values = malloc(vm * sizeof(char *)); + // since malloc(0) can be NULL on some platforms + char **values = malloc(1); DcmElement *element = dcm_element_create(NULL, tag, DCM_VR_CS); (void) dcm_element_set_value_string_multi(NULL, element, values, vm, true); @@ -528,15 +529,15 @@ END_TEST START_TEST(test_element_US_multivalue_empty) { uint32_t tag = 0x00280010; - uint16_t value[] = {}; - uint32_t vm = sizeof(value) / sizeof(value[0]); + // msvc hates zero length arrays, so use 1 + uint16_t value[] = {0}; DcmElement *element = dcm_element_create(NULL, tag, DCM_VR_US); - (void) dcm_element_set_value_numeric_multi(NULL, element, &value, vm, false); + (void) dcm_element_set_value_numeric_multi(NULL, element, &value, 0, false); ck_assert_int_eq(dcm_element_get_tag(element), tag); ck_assert_int_eq(dcm_element_get_vr(element), DCM_VR_US); - ck_assert_int_eq(dcm_element_get_length(element), sizeof(value)); + ck_assert_int_eq(dcm_element_get_length(element), 0); ck_assert_int_eq(dcm_element_is_multivalued(element), false); dcm_element_print(element, 0); @@ -739,7 +740,7 @@ START_TEST(test_file_sm_image_file_meta_memory) DcmElement *element; const char *value; - long length; + int64_t length; char *memory = load_file_to_memory("data/test_files/sm_image.dcm", &length); ck_assert_ptr_nonnull(memory);