@@ -109,6 +109,7 @@ test_json_struct_metadata_get_blob(void)
109109 tsk_size_t metadata_length ;
110110 size_t header_length ;
111111 size_t json_length ;
112+ size_t padding_length ;
112113 size_t payload_length ;
113114 size_t total_length ;
114115 char json_payload [] = "{\"a\":1}" ;
@@ -118,8 +119,9 @@ test_json_struct_metadata_get_blob(void)
118119 bytes = (uint8_t * ) metadata ;
119120 header_length = 4 + 1 + 8 + 8 ;
120121 json_length = strlen (json_payload );
122+ padding_length = (8 - ((header_length + json_length ) & 0x07 )) % 8 ;
121123 payload_length = sizeof (binary_payload );
122- total_length = header_length + json_length + payload_length ;
124+ total_length = header_length + json_length + padding_length + payload_length ;
123125 CU_ASSERT_FATAL (total_length <= sizeof (metadata ));
124126 memset (metadata , 0 , sizeof (metadata ));
125127 bytes [0 ] = 'J' ;
@@ -130,54 +132,64 @@ test_json_struct_metadata_get_blob(void)
130132 set_u64_le (bytes + 5 , (uint64_t ) json_length );
131133 set_u64_le (bytes + 13 , (uint64_t ) payload_length );
132134 memcpy (bytes + header_length , json_payload , json_length );
133- memcpy (bytes + header_length + json_length , binary_payload , payload_length );
135+ memset (bytes + header_length + json_length , 0 , padding_length );
136+ memcpy (bytes + header_length + json_length + padding_length , binary_payload ,
137+ payload_length );
134138 metadata_length = (tsk_size_t ) total_length ;
135139 ret = tsk_json_struct_metadata_get_blob (
136140 metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
137141 CU_ASSERT_EQUAL (ret , 0 );
138142 CU_ASSERT_PTR_EQUAL (json , (char * ) bytes + header_length );
143+ CU_ASSERT_EQUAL (json + json_buffer_length + padding_length , blob );
139144 CU_ASSERT_EQUAL (json_buffer_length , (tsk_size_t ) json_length );
140145 if (json_length > 0 ) {
141146 CU_ASSERT_EQUAL (memcmp (json , json_payload , json_length ), 0 );
142147 }
143- CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length );
148+ CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length + padding_length );
144149 CU_ASSERT_EQUAL (blob_length , (tsk_size_t ) payload_length );
145150 CU_ASSERT_EQUAL (memcmp (blob , binary_payload , payload_length ), 0 );
151+ CU_ASSERT ((tsk_size_t ) (blob - json ) < json_buffer_length + 8 );
146152
147153 payload_length = 0 ;
148- total_length = header_length + json_length + payload_length ;
154+ total_length = header_length + json_length + padding_length + payload_length ;
149155 CU_ASSERT_FATAL (total_length <= sizeof (metadata ));
150156 set_u64_le (bytes + 13 , (uint64_t ) payload_length );
151157 metadata_length = (tsk_size_t ) total_length ;
152158 ret = tsk_json_struct_metadata_get_blob (
153159 metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
154160 CU_ASSERT_EQUAL (ret , 0 );
161+ CU_ASSERT_EQUAL (json + json_buffer_length + padding_length , blob );
155162 CU_ASSERT_PTR_EQUAL (json , (char * ) bytes + header_length );
156163 CU_ASSERT_EQUAL (json_buffer_length , (tsk_size_t ) json_length );
157164 CU_ASSERT_EQUAL (blob_length , (tsk_size_t ) payload_length );
158- CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length );
165+ CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length + padding_length );
166+ CU_ASSERT ((tsk_size_t ) (blob - json ) < json_buffer_length + 8 );
159167
160168 json_length = 0 ;
161169 payload_length = sizeof (empty_payload );
162- total_length = header_length + json_length + payload_length ;
170+ padding_length = (8 - ((header_length + json_length ) & 0x07 )) % 8 ;
171+ total_length = header_length + json_length + padding_length + payload_length ;
163172 CU_ASSERT_FATAL (total_length <= sizeof (metadata ));
164173 set_u64_le (bytes + 5 , (uint64_t ) json_length );
165174 set_u64_le (bytes + 13 , (uint64_t ) payload_length );
166- memcpy (bytes + header_length + json_length , empty_payload , payload_length );
175+ memset (bytes + header_length + json_length , 0 , padding_length );
176+ memcpy (bytes + header_length + json_length + padding_length , empty_payload ,
177+ payload_length );
167178 metadata_length = (tsk_size_t ) total_length ;
168179 ret = tsk_json_struct_metadata_get_blob (
169180 metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
170181 CU_ASSERT_EQUAL (ret , 0 );
171182 CU_ASSERT_PTR_EQUAL (json , (char * ) bytes + header_length );
172183 CU_ASSERT_EQUAL (json_buffer_length , (tsk_size_t ) json_length );
173184 CU_ASSERT_EQUAL (blob_length , (tsk_size_t ) payload_length );
174- CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length );
185+ CU_ASSERT_PTR_EQUAL (blob , bytes + header_length + json_length + padding_length );
175186 CU_ASSERT_EQUAL (memcmp (blob , empty_payload , payload_length ), 0 );
187+ CU_ASSERT ((tsk_size_t ) (blob - json ) < json_buffer_length + 8 );
176188
177189 blob = NULL ;
178190 blob_length = 0 ;
179191 json = NULL ;
180- json_buffer_length = 0 ;
192+ json_length = 0 ;
181193 metadata_length = header_length - 1 ;
182194 ret = tsk_json_struct_metadata_get_blob (
183195 metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
@@ -196,7 +208,19 @@ test_json_struct_metadata_get_blob(void)
196208 CU_ASSERT_EQUAL (ret , TSK_ERR_JSON_STRUCT_METADATA_BAD_VERSION );
197209 bytes [4 ] = 1 ;
198210
199- metadata_length = (tsk_size_t ) (total_length - 1 );
211+ set_u64_le (bytes + 5 , (uint64_t ) json_length + 9 );
212+ ret = tsk_json_struct_metadata_get_blob (
213+ metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
214+ CU_ASSERT_EQUAL (ret , TSK_ERR_JSON_STRUCT_METADATA_UNEXPECTED_SIZE );
215+ set_u64_le (bytes + 5 , (uint64_t ) json_length );
216+
217+ bytes [header_length + 1 ] = 1 ;
218+ ret = tsk_json_struct_metadata_get_blob (
219+ metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
220+ CU_ASSERT_EQUAL (ret , TSK_ERR_JSON_STRUCT_METADATA_NONZERO_PADDING );
221+ bytes [header_length + 1 ] = 0 ;
222+
223+ metadata_length = (tsk_size_t ) (header_length - 1 );
200224 ret = tsk_json_struct_metadata_get_blob (
201225 metadata , metadata_length , & json , & json_buffer_length , & blob , & blob_length );
202226 CU_ASSERT_EQUAL (ret , TSK_ERR_JSON_STRUCT_METADATA_TRUNCATED );
0 commit comments