Skip to content

Commit 0b30978

Browse files
authored
Merge pull request #220 from saxbophone/josh/add-conversion-warnings
Enable clang/GCC -Wconversion flag
2 parents 63d0eed + 1c2b977 commit 0b30978

File tree

9 files changed

+82
-69
lines changed

9 files changed

+82
-69
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ os:
1111
compiler:
1212
- clang
1313
- gcc
14-
# different C Standard versions - test on C99 and C11
15-
env:
16-
matrix:
17-
- SXBP_C_STANDARD=99
18-
- SXBP_C_STANDARD=11
1914
cache:
2015
- ccache
2116
addons:

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ add_definitions(-DSXBP_VERSION_STRING=${SXBP_ESCAPED_VERSION_STRING})
6060
include(CheckCCompilerFlag)
6161

6262
function(enable_c_compiler_flag_if_supported flag)
63+
message(STATUS "[sxbp] Checking if compiler supports warning flag '${flag}'")
6364
string(FIND "${CMAKE_C_FLAGS}" "${flag}" flag_already_set)
6465
if(flag_already_set EQUAL -1)
6566
check_c_compiler_flag("${flag}" flag_supported)
6667
if(flag_supported)
67-
message(STATUS "[sxbp] Enabling warning flag ${flag}")
68+
message(STATUS "[sxbp] Enabling warning flag '${flag}'")
6869
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
6970
endif()
7071
endif()
@@ -85,10 +86,14 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "")
8586
enable_c_compiler_flag_if_supported("-Wstrict-prototypes")
8687
# enable warnings on missing prototypes of global functions
8788
enable_c_compiler_flag_if_supported("-Wmissing-prototypes")
88-
# turn off spurious warnings from clang about missing field initialisers
89-
enable_c_compiler_flag_if_supported("-Wno-missing-field-initializers")
89+
# enable sign and type conversion warnings
90+
enable_c_compiler_flag_if_supported("-Wsign-conversion")
91+
# enable warnings about C11 features not supported in C99
92+
enable_c_compiler_flag_if_supported("-Wc99-c11-compat")
9093
# enable warnings about mistakes in Doxygen documentation
9194
enable_c_compiler_flag_if_supported("-Wdocumentation")
95+
# turn off spurious warnings from clang about missing field initialisers
96+
enable_c_compiler_flag_if_supported("-Wno-missing-field-initializers")
9297
# convert all warnings into errors
9398
enable_c_compiler_flag_if_supported("-Werror")
9499
endif()

sxbp/begin_figure.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static sxbp_direction_t sxbp_change_line_direction(
5555
sxbp_direction_t current,
5656
sxbp_rotation_t turn
5757
) {
58-
return (current + turn) % 4;
58+
return (current + (sxbp_direction_t)turn) % 4;
5959
}
6060

6161
/*
@@ -72,13 +72,13 @@ static sxbp_length_t sxbp_next_length(
7272
assert(direction <= SXBP_LEFT);
7373
switch (direction % 4u) {
7474
case SXBP_UP:
75-
return abs(bounds.y_max - location.y) + 1;
75+
return labs(bounds.y_max - location.y) + 1;
7676
case SXBP_RIGHT:
77-
return abs(bounds.x_max - location.x) + 1;
77+
return labs(bounds.x_max - location.x) + 1;
7878
case SXBP_DOWN:
79-
return abs(bounds.y_min - location.y) + 1;
79+
return labs(bounds.y_min - location.y) + 1;
8080
case SXBP_LEFT:
81-
return abs(bounds.x_min - location.x) + 1;
81+
return labs(bounds.x_min - location.x) + 1;
8282
default:
8383
/*
8484
* NOTE: this case should never happen

sxbp/render_figure_to_bitmap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
1414
*/
1515
#include <stdbool.h>
16+
#include <stdint.h>
1617

1718
#include "sxbp.h"
1819
#include "sxbp_internal.h"
@@ -39,7 +40,8 @@ static bool sxbp_render_figure_to_bitmap_callback(
3940
void* callback_data
4041
) {
4142
// cast void pointer to a pointer to our context structure
42-
render_figure_to_bitmap_context* data = (render_figure_to_bitmap_context*)callback_data;
43+
render_figure_to_bitmap_context* data =
44+
(render_figure_to_bitmap_context*)callback_data;
4345
// skip the plotting of the second pixel
4446
if (data->first_pixel_complete && !data->second_pixel_complete) {
4547
// mark second pixel as complete
@@ -48,7 +50,9 @@ static bool sxbp_render_figure_to_bitmap_callback(
4850
// mark first pixel as complete if it isn't already
4951
data->first_pixel_complete = true;
5052
// plot the pixel, but flip the y coördinate
51-
data->image->pixels[location.x][data->image->height - 1 - location.y] = true;
53+
data->image
54+
->pixels[location.x]
55+
[data->image->height - 1 - (uint32_t)location.y] = true;
5256
}
5357
// return true --we always want to continue
5458
return true;

sxbp/render_figure_to_pbm.c

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,52 +63,60 @@ static sxbp_result_t sxbp_write_pbm_header(
6363
* char arrays of 11 chars each (1 extra char for null-terminator)
6464
*/
6565
char width_string[11], height_string[11];
66-
// these are used to keep track of how many digits each is
67-
int width_string_length, height_string_length = 0;
68-
// convert width and height to a decimal string, store lengths
69-
width_string_length = snprintf(
66+
// we'll store the return values of two snprintf() calls in these variables
67+
int width_string_result, height_string_result = 0;
68+
// convert width and height to a decimal string, check for errors
69+
width_string_result = snprintf(
7070
width_string, 11, "%" PRIu32, bitmap->width
7171
);
72-
height_string_length = snprintf(
72+
height_string_result = snprintf(
7373
height_string, 11, "%" PRIu32, bitmap->height
7474
);
75-
/*
76-
* now that we know the length of the image dimension strings, we can now
77-
* calculate how much memory we'll have to allocate for the image buffer
78-
*/
79-
buffer->size = sxbp_get_pbm_image_size(
80-
bitmap->width,
81-
bitmap->height,
82-
width_string_length,
83-
height_string_length,
84-
bytes_per_row_ptr
85-
);
86-
// try and allocate the buffer
87-
if (!sxbp_check(sxbp_init_buffer(buffer), &error)) {
88-
// catch and return error if there was one
89-
return error;
75+
if (width_string_result < 0 || height_string_result < 0) {
76+
// snprintf() returns negative values when it fails, so return an error
77+
return SXBP_RESULT_FAIL_IO;
9078
} else {
91-
// now with the buffer allocated, write the header
92-
size_t index = *index_ptr; // this index is used to index the buffer
93-
// construct magic number + whitespace
94-
memcpy(buffer->bytes + index, "P4\n", 3);
95-
index += 3;
96-
// image width
97-
memcpy(buffer->bytes + index, width_string, width_string_length);
98-
index += width_string_length;
99-
// whitespace
100-
memcpy(buffer->bytes + index, "\n", 1);
101-
index += 1;
102-
// image height
103-
memcpy(buffer->bytes + index, height_string, height_string_length);
104-
index += height_string_length;
105-
// whitespace
106-
memcpy(buffer->bytes + index, "\n", 1);
107-
index += 1;
108-
// update the index pointer
109-
*index_ptr = index;
79+
// get lengths from snprintf() return values
80+
size_t width_string_length = (size_t)width_string_result;
81+
size_t height_string_length = (size_t)height_string_result;
82+
/*
83+
* now that we know the length of the image dimension strings, we can now
84+
* calculate how much memory we'll have to allocate for the image buffer
85+
*/
86+
buffer->size = sxbp_get_pbm_image_size(
87+
bitmap->width,
88+
bitmap->height,
89+
width_string_length,
90+
height_string_length,
91+
bytes_per_row_ptr
92+
);
93+
// try and allocate the buffer
94+
if (!sxbp_check(sxbp_init_buffer(buffer), &error)) {
95+
// catch and return error if there was one
96+
return error;
97+
} else {
98+
// now with the buffer allocated, write the header
99+
size_t index = *index_ptr; // this index is used to index the buffer
100+
// construct magic number + whitespace
101+
memcpy(buffer->bytes + index, "P4\n", 3);
102+
index += 3;
103+
// image width
104+
memcpy(buffer->bytes + index, width_string, width_string_length);
105+
index += width_string_length;
106+
// whitespace
107+
memcpy(buffer->bytes + index, "\n", 1);
108+
index += 1;
109+
// image height
110+
memcpy(buffer->bytes + index, height_string, height_string_length);
111+
index += height_string_length;
112+
// whitespace
113+
memcpy(buffer->bytes + index, "\n", 1);
114+
index += 1;
115+
// update the index pointer
116+
*index_ptr = index;
117+
}
118+
return SXBP_RESULT_OK;
110119
}
111-
return SXBP_RESULT_OK;
112120
}
113121

114122
// private, writes the image data out to the buffer

sxbp/serialisation.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static void sxbp_write_sxbp_data_line(
130130
) {
131131
size_t index = *start_index;
132132
// the leading 2 bits of the first byte are used for the direction
133-
buffer->bytes[index] = (line.direction << 6);
133+
buffer->bytes[index] = line.direction << 6;
134134
// the next 6 bits of the first byte are used for the first 6 bits of length
135135
buffer->bytes[index] |= (line.length >> 24);
136136
// move on to the next byte
@@ -171,7 +171,8 @@ static uint16_t sxbp_load_uint16_t(
171171
size_t index = *start_index;
172172
// extract the value
173173
uint16_t value = (
174-
((uint16_t)buffer->bytes[index] << 8) + buffer->bytes[index + 1]
174+
(uint16_t)((uint16_t)buffer->bytes[index] << 8) +
175+
buffer->bytes[index + 1]
175176
);
176177
// increment index to point to next location
177178
*start_index += 2;
@@ -195,7 +196,7 @@ static uint32_t sxbp_load_uint32_t(
195196
// extract the value
196197
uint32_t value = 0;
197198
for(uint8_t i = 0; i < 4; i++) {
198-
value |= (buffer->bytes[index + i]) << (8 * (3 - i));
199+
value |= (uint8_t)((buffer->bytes[index + i]) << (8 * (3 - i)));
199200
}
200201
// increment index to point to next location
201202
*start_index += 4;
@@ -255,7 +256,7 @@ static void sxbp_read_sxbp_data_line(
255256
* handle first byte on it's own as we only need least 6 bits of it
256257
* bit mask and shift 3 bytes to left
257258
*/
258-
line->length = (buffer->bytes[index] & 0x3f) << 24; // 0x3f = 0b00111111
259+
line->length = (buffer->bytes[index] & 0x3fU) << 24; // 0x3f = 0b00111111
259260
// next byte
260261
index++;
261262
// handle remaining 3 bytes in loop

sxbp/sxbp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ typedef enum sxbp_result_t {
228228
SXBP_RESULT_OK, /**< success */
229229
SXBP_RESULT_FAIL_MEMORY, /**< failure to allocate or reallocate memory */
230230
SXBP_RESULT_FAIL_PRECONDITION, /**< a preconditional check failed */
231-
SXBP_RESULT_FAIL_FILE, /**< a file read/write operation failed */
231+
SXBP_RESULT_FAIL_IO, /**< an input/output operation failed */
232232
SXBP_RESULT_FAIL_UNIMPLEMENTED, /**< requested action is not implemented */
233233
SXBP_RESULT_RESERVED_START, /**< reserved for future use */
234234
SXBP_RESULT_RESERVED_END = 255u, /**< reserved for future use */
@@ -356,7 +356,7 @@ sxbp_result_t sxbp_copy_buffer(
356356
* @param file_handle The file to read data from
357357
* @param[out] buffer The buffer to write data to
358358
* @returns `SXBP_RESULT_OK` on successfully copying the file contents
359-
* @returns `SXBP_RESULT_FAIL_MEMORY` or `SXBP_RESULT_FAIL_FILE` on failure to copy the file contents
359+
* @returns `SXBP_RESULT_FAIL_MEMORY` or `SXBP_RESULT_FAIL_IO` on failure to copy the file contents
360360
* @returns `SXBP_RESULT_FAIL_PRECONDITION` if `file_handle` or `buffer` is
361361
* `NULL`
362362
* @since v0.54.0
@@ -373,7 +373,7 @@ sxbp_result_t sxbp_buffer_from_file(
373373
* @param buffer The buffer to read data from
374374
* @param[out] file_handle The file to write data to
375375
* @returns `SXBP_RESULT_OK` on successfully writing the file
376-
* @returns `SXBP_RESULT_FAIL_FILE` on failure to write the file
376+
* @returns `SXBP_RESULT_FAIL_IO` on failure to write the file
377377
* @returns `SXBP_RESULT_FAIL_PRECONDITION` if `buffer` or `file_handle` is
378378
* `NULL`
379379
* @since v0.54.0

sxbp/sxbp_internal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void sxbp_move_location(
5757
sxbp_length_t length
5858
) {
5959
sxbp_vector_t vector = SXBP_VECTOR_DIRECTIONS[direction];
60-
location->x += vector.x * length;
61-
location->y += vector.y * length;
60+
location->x += vector.x * (sxbp_tuple_item_t)length;
61+
location->y += vector.y * (sxbp_tuple_item_t)length;
6262
}
6363

6464
void sxbp_move_location_along_line(
@@ -141,8 +141,8 @@ sxbp_result_t sxbp_make_bitmap_for_bounds(
141141
* this makes sense because for example from 1 to 10 there are 10 values
142142
* and the difference of these is 9 so the number of values is 9+1 = 10
143143
*/
144-
bitmap->width = (bounds.x_max - bounds.x_min) + 1;
145-
bitmap->height = (bounds.y_max - bounds.y_min) + 1;
144+
bitmap->width = (uint32_t)((bounds.x_max - bounds.x_min) + 1);
145+
bitmap->height = (uint32_t)((bounds.y_max - bounds.y_min) + 1);
146146
bitmap->pixels = NULL;
147147
// allocate memory for the bitmap and return the status of this operation
148148
return sxbp_init_bitmap(bitmap);

sxbp/utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static size_t sxbp_get_file_size(FILE* file_handle) {
102102
// NOTE: This isn't portable due to lack of meaningful support of `SEEK_END`
103103
fseek(file_handle, 0, SEEK_END);
104104
// get size
105-
size_t file_size = ftell(file_handle);
105+
size_t file_size = (size_t)ftell(file_handle);
106106
// seek to start again
107107
fseek(file_handle, 0, SEEK_SET);
108108
return file_size;
@@ -139,7 +139,7 @@ sxbp_result_t sxbp_buffer_from_file(
139139
// we didn't read the same number of bytes as the file's size
140140
sxbp_free_buffer(buffer);
141141
// return a file error
142-
return SXBP_RESULT_FAIL_FILE;
142+
return SXBP_RESULT_FAIL_IO;
143143
} else {
144144
// we read the buffer successfully, so return success
145145
return SXBP_RESULT_OK;
@@ -162,7 +162,7 @@ sxbp_result_t sxbp_buffer_to_file(
162162
file_handle
163163
);
164164
// return success/failure if the correct number of bytes were written
165-
return bytes_written == buffer->size ? SXBP_RESULT_OK : SXBP_RESULT_FAIL_FILE;
165+
return bytes_written == buffer->size ? SXBP_RESULT_OK : SXBP_RESULT_FAIL_IO;
166166
}
167167

168168
sxbp_figure_t sxbp_blank_figure(void) {

0 commit comments

Comments
 (0)