fix several problems caused by insufficient error checking and some double

Qclose calls
This commit is contained in:
Bill Currie 2002-08-15 18:53:03 +00:00
parent 00c6d011e7
commit 36ca7b55bd
4 changed files with 27 additions and 16 deletions

View file

@ -33,8 +33,8 @@
#define __linker_h #define __linker_h
void linker_begin (void); void linker_begin (void);
void linker_add_object_file (const char *filename); int linker_add_object_file (const char *filename);
void linker_add_lib (const char *libname); int linker_add_lib (const char *libname);
struct qfo_s *linker_finish (void); struct qfo_s *linker_finish (void);
#endif//__linker_h #endif//__linker_h

View file

@ -566,14 +566,14 @@ linker_add_qfo (qfo_t *qfo)
add_lines (qfo); add_lines (qfo);
} }
void int
linker_add_object_file (const char *filename) linker_add_object_file (const char *filename)
{ {
qfo_t *qfo; qfo_t *qfo;
qfo = qfo_open (filename); qfo = qfo_open (filename);
if (!qfo) if (!qfo)
return; return 1;
if (options.verbosity >= 2) if (options.verbosity >= 2)
puts (filename); puts (filename);
@ -581,9 +581,10 @@ linker_add_object_file (const char *filename)
linker_add_qfo (qfo); linker_add_qfo (qfo);
qfo_delete (qfo); qfo_delete (qfo);
return 0;
} }
void int
linker_add_lib (const char *libname) linker_add_lib (const char *libname)
{ {
pack_t *pack = pack_open (libname); pack_t *pack = pack_open (libname);
@ -591,7 +592,7 @@ linker_add_lib (const char *libname)
int did_something; int did_something;
if (!pack) if (!pack)
return; return 1;
do { do {
did_something = 0; did_something = 0;
for (i = 0; i < pack->numfiles; i++) { for (i = 0; i < pack->numfiles; i++) {
@ -606,6 +607,9 @@ linker_add_lib (const char *libname)
Qclose (f); Qclose (f);
close (fd); close (fd);
if (!qfo)
return 1;
for (j = 0; j < qfo->num_defs; j++) { for (j = 0; j < qfo->num_defs; j++) {
qfo_def_t *def = qfo->defs + j; qfo_def_t *def = qfo->defs + j;
if ((def->flags & QFOD_GLOBAL) if ((def->flags & QFOD_GLOBAL)
@ -623,6 +627,7 @@ linker_add_lib (const char *libname)
} }
} while (did_something); } while (did_something);
pack_del (pack); pack_del (pack);
return 0;
} }
qfo_t * qfo_t *

View file

@ -339,7 +339,6 @@ qfo_read (VFile *file)
if (strcmp (hdr.qfo, QFO)) { if (strcmp (hdr.qfo, QFO)) {
fprintf (stderr, "not a qfo file\n"); fprintf (stderr, "not a qfo file\n");
Qclose (file);
return 0; return 0;
} }
@ -361,7 +360,6 @@ qfo_read (VFile *file)
fprintf (stderr, "can't read version %x.%03x.%03x\n", fprintf (stderr, "can't read version %x.%03x.%03x\n",
(hdr.version >> 24) & 0xff, (hdr.version >> 12) & 0xfff, (hdr.version >> 24) & 0xff, (hdr.version >> 12) & 0xfff,
hdr.version & 0xfff); hdr.version & 0xfff);
Qclose (file);
free (qfo); free (qfo);
return 0; return 0;
} }
@ -374,7 +372,8 @@ qfo_read (VFile *file)
qfo->relocs = malloc (qfo->num_relocs * sizeof (qfo_reloc_t)); qfo->relocs = malloc (qfo->num_relocs * sizeof (qfo_reloc_t));
qfo->defs = malloc (qfo->num_defs * sizeof (qfo_def_t)); qfo->defs = malloc (qfo->num_defs * sizeof (qfo_def_t));
qfo->funcs = malloc (qfo->num_funcs * sizeof (qfo_func_t)); qfo->funcs = malloc (qfo->num_funcs * sizeof (qfo_func_t));
qfo->lines = malloc (qfo->num_lines * sizeof (pr_lineno_t)); if (qfo->num_lines)
qfo->lines = malloc (qfo->num_lines * sizeof (pr_lineno_t));
qfo->types = malloc (qfo->types_size); qfo->types = malloc (qfo->types_size);
Qread (file, qfo->code, qfo->code_size * sizeof (dstatement_t)); Qread (file, qfo->code, qfo->code_size * sizeof (dstatement_t));
@ -463,8 +462,10 @@ init_space (int size, pr_type_t *data)
defspace_t *space = new_defspace (); defspace_t *space = new_defspace ();
space->size = size; space->size = size;
space->max_size = RUP (space->size, 65536); space->max_size = RUP (space->size, 65536);
if (!space->max_size)
space->max_size=65536;
space->data = malloc (space->max_size * sizeof (pr_type_t));
if (size && data) { if (size && data) {
space->data = malloc (space->max_size * sizeof (pr_type_t));
memcpy (space->data, data, size * sizeof (pr_type_t)); memcpy (space->data, data, size * sizeof (pr_type_t));
} }
return space; return space;
@ -478,9 +479,10 @@ qfo_to_progs (qfo_t *qfo, pr_info_t *pr)
qfo_func_t *qf; qfo_func_t *qf;
def_t *pd; def_t *pd;
qfo_def_t *qd; qfo_def_t *qd;
reloc_t *relocs; reloc_t *relocs = 0;
relocs = calloc (qfo->num_relocs, sizeof (reloc_t)); if (qfo->num_relocs)
relocs = calloc (qfo->num_relocs, sizeof (reloc_t));
for (i = 0; i < qfo->num_relocs; i++) { for (i = 0; i < qfo->num_relocs; i++) {
if (i + 1 < qfo->num_relocs) if (i + 1 < qfo->num_relocs)
relocs[i].next = &relocs[i + 1]; relocs[i].next = &relocs[i + 1];
@ -498,7 +500,8 @@ qfo_to_progs (qfo_t *qfo, pr_info_t *pr)
pr->entity_data = new_defspace (); pr->entity_data = new_defspace ();
pr->scope = new_scope (sc_global, pr->near_data, 0); pr->scope = new_scope (sc_global, pr->near_data, 0);
pr->scope->num_defs = qfo->num_defs; pr->scope->num_defs = qfo->num_defs;
pr->scope->head = calloc (pr->scope->num_defs, sizeof (def_t)); if (qfo->num_defs)
pr->scope->head = calloc (pr->scope->num_defs, sizeof (def_t));
for (i = 0, pd = pr->scope->head, qd = qfo->defs; for (i = 0, pd = pr->scope->head, qd = qfo->defs;
i < pr->scope->num_defs; i++, pd++, qd++) { i < pr->scope->num_defs; i++, pd++, qd++) {
*pr->scope->tail = pd; *pr->scope->tail = pd;
@ -529,7 +532,8 @@ qfo_to_progs (qfo_t *qfo, pr_info_t *pr)
} }
pr->num_functions = qfo->num_funcs + 1; pr->num_functions = qfo->num_funcs + 1;
pr->func_head = calloc (qfo->num_funcs, sizeof (function_t)); if (qfo->num_funcs)
pr->func_head = calloc (qfo->num_funcs, sizeof (function_t));
pr->func_tail = &pr->func_head; pr->func_tail = &pr->func_head;
for (i = 0, pf = pr->func_head, qf = qfo->funcs; for (i = 0, pf = pr->func_head, qf = qfo->funcs;
i < qfo->num_funcs; i++, pf++, qf++) { i < qfo->num_funcs; i++, pf++, qf++) {

View file

@ -551,9 +551,11 @@ separate_compile (void)
linker_begin (); linker_begin ();
for (file = source_files; *file; file++) { for (file = source_files; *file; file++) {
if (strncmp (*file, "-l", 2)) if (strncmp (*file, "-l", 2))
linker_add_object_file (*file); err = linker_add_object_file (*file);
else else
linker_add_lib (*file + 2); err = linker_add_lib (*file + 2);
if (err)
return err;
} }
qfo = linker_finish (); qfo = linker_finish ();
if (qfo) { if (qfo) {