44#include <stdint.h>
55#include <string.h>
66#include <errno.h>
7- #include "io.h"
87#include "list.h"
98#include "stack.h"
109#include "expression.h"
@@ -257,42 +256,79 @@ area_t *read_area(FILE *f) {
257256 scas_log (L_DEBUG , "Reading area '%s' from file" , name );
258257 free (name );
259258 uint32_t symbols , immediates ;
260- scas_read (& symbols , sizeof (uint32_t ), 1 , f );
259+ if (fread (& symbols , 1 , sizeof (uint32_t ), f ) != sizeof (uint32_t )) {
260+ scas_log (L_ERROR , "Failed to read area from file" );
261+ area_free (area );
262+ return NULL ;
263+ }
261264 uint32_t len ;
262265 for (uint32_t i = 0 ; i < symbols ; ++ i ) {
263266 symbol_t * sym = malloc (sizeof (symbol_t ));
264267 sym -> exported = fgetc (f );
265- scas_read (& len , sizeof (uint32_t ), 1 , f );
268+ if (fread (& len , sizeof (uint32_t ), 1 , f ) != 1 ) {
269+ scas_log (L_ERROR , "TODO FIXME" );
270+ return NULL ;
271+ }
266272 sym -> name = calloc (len + 1 , sizeof (char ));
267- scas_read (sym -> name , sizeof (char ), len , f );
268- scas_read (& sym -> value , sizeof (uint64_t ), 1 , f );
269- scas_read (& sym -> defined_address , sizeof (uint64_t ), 1 , f );
273+ if (fread (sym -> name , 1 , len , f ) != len ) {
274+ scas_log (L_ERROR , "TODO FIXME" );
275+ return NULL ;
276+ }
277+ if (fread (& sym -> value , 1 , sizeof (uint64_t ), f ) != sizeof (uint64_t )) {
278+ scas_log (L_ERROR , "TODO FIXME" );
279+ return NULL ;
280+ }
281+ if (fread (& sym -> defined_address , 1 , sizeof (uint64_t ), f ) != sizeof (uint64_t )) {
282+ scas_log (L_ERROR , "TODO FIXME" );
283+ return NULL ;
284+ }
270285 sym -> type = SYMBOL_LABEL ;
271286 list_add (area -> symbols , sym );
272287 scas_log (L_DEBUG , "Read symbol '%s' with value 0x%08X%08X" , sym -> name , (uint32_t )(sym -> value >> 32 ), (uint32_t )sym -> value );
273288 }
274289 /* TODO: Imports */
275- scas_read (& immediates , sizeof (uint32_t ), 1 , f );
290+ if (fread (& immediates , 1 , sizeof (uint32_t ), f ) != sizeof (uint32_t )) {
291+ scas_log (L_ERROR , "TODO FIXME" );
292+ return NULL ;
293+ }
276294 for (uint32_t i = 0 ; i < immediates ; ++ i ) {
277295 late_immediate_t * imm = malloc (sizeof (late_immediate_t ));
278296 imm -> type = fgetc (f );
279297 imm -> width = fgetc (f );
280- scas_read (& imm -> instruction_address , sizeof (uint64_t ), 1 , f );
281- scas_read (& imm -> base_address , sizeof (uint64_t ), 1 , f );
282- scas_read (& imm -> address , sizeof (uint64_t ), 1 , f );
298+ if (fread (& imm -> instruction_address , sizeof (uint64_t ), 1 , f ) != 1 ) {
299+ scas_log (L_ERROR , "TODO FIXME" );
300+ return NULL ;
301+ }
302+ if (fread (& imm -> base_address , sizeof (uint64_t ), 1 , f ) != 1 ) {
303+ scas_log (L_ERROR , "TODO FIXME" );
304+ return NULL ;
305+ }
306+ if (fread (& imm -> address , sizeof (uint64_t ), 1 , f ) != 1 ) {
307+ scas_log (L_ERROR , "TODO FIXME" );
308+ return NULL ;
309+ }
283310 imm -> expression = fread_tokenized_expression (f );
284311 list_add (area -> late_immediates , imm );
285312 scas_log (L_DEBUG , "Read immediate value at 0x%08X (width: %d)" , imm -> address , imm -> width );
286313 }
287- scas_read (& area -> data_length , sizeof (uint64_t ), 1 , f );
314+ if (fread (& area -> data_length , sizeof (uint64_t ), 1 , f ) != 1 ) {
315+ scas_log (L_ERROR , "TODO FIXME" );
316+ return NULL ;
317+ }
288318 area -> data_capacity = area -> data_length ;
289319 free (area -> data );
290320 area -> data = malloc ((int )area -> data_length );
291- scas_read (area -> data , sizeof (uint8_t ), (int )area -> data_length , f );
321+ if (fread (area -> data , sizeof (uint8_t ), (int )area -> data_length , f ) != 1 ) {
322+ scas_log (L_ERROR , "TODO FIXME" );
323+ return NULL ;
324+ }
292325 scas_log (L_DEBUG , "Read %d bytes of machine code" , area -> data_length );
293326
294327 uint64_t meta_length , meta_key ;
295- scas_read (& meta_length , sizeof (uint64_t ), 1 , f );
328+ if (fread (& meta_length , sizeof (uint64_t ), 1 , f ) != 1 ) {
329+ scas_log (L_ERROR , "TODO FIXME" );
330+ return NULL ;
331+ }
296332 meta_length = (int )meta_length ;
297333 scas_log (L_DEBUG , "Reading %d metadata entries" , meta_length );
298334 for (uint64_t i = 0 ; i < meta_length ; ++ i ) {
@@ -301,29 +337,53 @@ area_t *read_area(FILE *f) {
301337 meta_key = fgetc (f );
302338 meta -> key = malloc (meta_key + 1 );
303339 meta -> key [meta_key ] = 0 ;
304- scas_read (meta -> key , sizeof (char ), meta_key , f );
305- scas_read (& meta -> value_length , sizeof (uint64_t ), 1 , f );
340+ if (fread (meta -> key , sizeof (char ), meta_key , f ) != 1 ) {
341+ scas_log (L_ERROR , "TODO FIXME" );
342+ return NULL ;
343+ }
344+ if (fread (& meta -> value_length , sizeof (uint64_t ), 1 , f ) != 1 ) {
345+ scas_log (L_ERROR , "TODO FIXME" );
346+ return NULL ;
347+ }
306348 meta -> value = malloc (meta -> value_length + 1 );
307349 meta -> value [meta -> value_length ] = 0 ;
308- scas_read (meta -> value , sizeof (char ), meta -> value_length , f );
350+ if (fread (meta -> value , sizeof (char ), meta -> value_length , f ) != 1 ) {
351+ scas_log (L_ERROR , "TODO FIXME" );
352+ return NULL ;
353+ }
309354 list_add (area -> metadata , meta );
310355 scas_log (L_DEBUG , "Read metadata %s with value length %d" , meta -> key , meta -> value_length );
311356 }
312357
313358 uint64_t fileno , lineno ;
314- scas_read (& fileno , sizeof (uint64_t ), 1 , f );
359+ if (fread (& fileno , sizeof (uint64_t ), 1 , f ) != 1 ) {
360+ scas_log (L_ERROR , "TODO FIXME" );
361+ return NULL ;
362+ }
315363 fileno = (int )fileno ;
316364 for (uint64_t i = 0 ; i < fileno ; ++ i ) {
317365 source_map_t * map = malloc (sizeof (source_map_t ));
318366 map -> file_name = read_line (f );
319367 map -> entries = create_list ();
320- scas_read (& lineno , sizeof (uint64_t ), 1 , f );
368+ if (fread (& lineno , sizeof (uint64_t ), 1 , f ) != 1 ) {
369+ scas_log (L_ERROR , "TODO FIXME" );
370+ return NULL ;
371+ }
321372 scas_log (L_DEBUG , "Reading source map for '%s', %d entries" , map -> file_name , lineno );
322373 for (uint64_t j = 0 ; j < lineno ; ++ j ) {
323374 source_map_entry_t * entry = malloc (sizeof (source_map_entry_t ));
324- scas_read (& entry -> line_number , sizeof (uint64_t ), 1 , f );
325- scas_read (& entry -> address , sizeof (uint64_t ), 1 , f );
326- scas_read (& entry -> length , sizeof (uint64_t ), 1 , f );
375+ if (fread (& entry -> line_number , sizeof (uint64_t ), 1 , f ) != 1 ) {
376+ scas_log (L_ERROR , "TODO FIXME" );
377+ return NULL ;
378+ }
379+ if (fread (& entry -> address , sizeof (uint64_t ), 1 , f ) != 1 ) {
380+ scas_log (L_ERROR , "TODO FIXME" );
381+ return NULL ;
382+ }
383+ if (fread (& entry -> length , sizeof (uint64_t ), 1 , f ) != 1 ) {
384+ scas_log (L_ERROR , "TODO FIXME" );
385+ return NULL ;
386+ }
327387 entry -> source_code = read_line (f );
328388 list_add (map -> entries , entry );
329389 scas_log (L_DEBUG , "Read entry at 0x%08X%08X (line %d): %s" , (uint32_t )(entry -> address >> 32 ), (uint32_t )entry -> address , entry -> line_number , entry -> source_code );
@@ -334,18 +394,23 @@ area_t *read_area(FILE *f) {
334394}
335395
336396object_t * freadobj (FILE * f , const char * name ) {
337- object_t * o = create_object ();
338397 char magic [7 ];
339398 int len = fread (magic , sizeof (char ), 7 , f );
340399 if (len != 7 || strncmp ("SCASOBJ" , magic , 7 ) != 0 ) {
341- scas_abort ("'%s' is not a valid object file." , name );
400+ scas_log (L_ERROR , "'%s' is not a valid object file." , name );
401+ return NULL ;
342402 }
343403 int ver = fgetc (f );
344404 if (ver != SCASOBJ_VERSION ) {
345- scas_abort ("'%s' was built with an incompatible version of scas." , name );
405+ scas_log (L_ERROR , "'%s' was built with an incompatible version of scas." , name );
406+ return NULL ;
346407 }
347408 uint32_t area_count ;
348- scas_read (& area_count , sizeof (uint32_t ), 1 , f );
409+ if (fread (& area_count , sizeof (uint32_t ), 1 , f ) != 1 ) {
410+ scas_log (L_ERROR , "TODO FIXME" );
411+ return NULL ;
412+ }
413+ object_t * o = create_object ();
349414 for (uint32_t i = 0 ; i < area_count ; ++ i ) {
350415 list_add (o -> areas , read_area (f ));
351416 }
0 commit comments