@@ -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
0 commit comments