Skip to content

Commit

Permalink
Use Parrot_ex_throw_from_c_args to report errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrajca committed Dec 31, 2010
1 parent e56a22f commit 56de7e3
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/packfile/api.c
Expand Up @@ -1010,21 +1010,18 @@ PackFile_unpack(PARROT_INTERP, ARGMOD(PackFile *self),

/* Check wordsize, byte order and floating point number type are valid. */
if (header->wordsize != 4 && header->wordsize != 8) {
Parrot_io_eprintf(NULL, "PackFile_unpack: Invalid wordsize %d\n",
header->wordsize);
return 0;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Invalid wordsize %d\n", header->wordsize);
}

if (header->byteorder != 0 && header->byteorder != 1) {
Parrot_io_eprintf(NULL, "PackFile_unpack: Invalid byte ordering %d\n",
header->byteorder);
return 0;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Invalid byte ordering %d\n", header->byteorder);
}

if (header->floattype > FLOATTYPE_MAX) {
Parrot_io_eprintf(NULL, "PackFile_unpack: Invalid floattype %d\n",
header->floattype);
return 0;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Invalid floattype %d\n", header->floattype);
}

/* Describe what was read for debugging. */
Expand All @@ -1045,10 +1042,9 @@ PackFile_unpack(PARROT_INTERP, ARGMOD(PackFile *self),
}
else if (header->uuid_type == 1) {
if (packed_size < (size_t) PACKFILE_HEADER_BYTES + header->uuid_size) {
Parrot_io_eprintf(NULL, "PackFile_unpack: "
"Buffer length %d is shorter than PACKFILE_HEADER_BYTES + uuid_size %d\n",
packed_size, PACKFILE_HEADER_BYTES + header->uuid_size);
return 0;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Buffer length %d is shorter than PACKFILE_HEADER_BYTES "
"+ uuid_size %d\n", packed_size, PACKFILE_HEADER_BYTES + header->uuid_size);
}


Expand All @@ -1065,8 +1061,8 @@ PackFile_unpack(PARROT_INTERP, ARGMOD(PackFile *self),
}
else
/* Don't know this UUID type. */
Parrot_io_eprintf(NULL, "PackFile_unpack: Invalid UUID type %d\n",
header->uuid_type);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Invalid UUID type %d\n", header->uuid_type);

/* Set cursor to position after what we've read, allowing for padding to a
* 16 byte boundary. */
Expand All @@ -1086,9 +1082,9 @@ PackFile_unpack(PARROT_INTERP, ARGMOD(PackFile *self),
header->dir_format = PF_fetch_opcode(self, &cursor);

if (header->dir_format != PF_DIR_FORMAT) {
Parrot_io_eprintf(NULL, "PackFile_unpack: Dir format was %d not %d\n",
header->dir_format, PF_DIR_FORMAT);
return 0;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack: Dir format was %d not %d\n",
header->dir_format, PF_DIR_FORMAT);
}

/* Padding. */
Expand Down Expand Up @@ -1458,9 +1454,8 @@ default_unpack(PARROT_INTERP, ARGMOD(PackFile_Segment *self), ARGIN(const opcode
self->data = mem_gc_allocate_n_typed(interp, self->size, opcode_t);

if (!self->data) {
Parrot_io_eprintf(NULL, "PackFile_unpack: Unable to allocate data memory!\n");
self->size = 0;
return NULL;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ALLOCATION_ERROR,
"PackFile_unpack: Unable to allocate data memory!\n");
}

if (!self->pf->need_endianize && !self->pf->need_wordsize) {
Expand Down Expand Up @@ -2046,10 +2041,10 @@ directory_unpack(PARROT_INTERP, ARGMOD(PackFile_Segment *segp), ARGIN(const opco
opcode = PF_fetch_opcode(pf, &pos);

if (seg->op_count != opcode) {
Parrot_io_eprintf(interp,
"%Ss: Size in directory %d doesn't match size %d "
"at offset 0x%x\n", seg->name, (int)seg->op_count,
(int)opcode, (int)seg->file_offset);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"%Ss: Size in directory %d doesn't match size %d "
"at offset 0x%x\n", seg->name, (int)seg->op_count,
(int)opcode, (int)seg->file_offset);
}

if (i) {
Expand Down Expand Up @@ -2088,9 +2083,8 @@ directory_unpack(PARROT_INTERP, ARGMOD(PackFile_Segment *segp), ARGIN(const opco
pos = PackFile_Segment_unpack(interp, dir->segments[i], cursor);

if (!pos) {
Parrot_io_eprintf(interp, "PackFile_unpack segment '%Ss' failed\n",
dir->segments[i]->name);
return NULL;
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_MALFORMED_PACKFILE,
"PackFile_unpack segment '%Ss' failed\n", dir->segments[i]->name);
}
else {
TRACE_PRINTF_VAL(("PackFile_Segment_unpack ok. pos=0x%x\n", pos));
Expand Down Expand Up @@ -3460,10 +3454,8 @@ PackFile_ConstTable_unpack(PARROT_INTERP, ARGIN(PackFile_Segment *seg),
return cursor;

err:
Parrot_io_eprintf(interp,
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ALLOCATION_ERROR,
"PackFile_ConstTable_unpack: Could not allocate memory for array!\n");
PackFile_ConstTable_clear(interp, self);
return NULL;
}


Expand Down

0 comments on commit 56de7e3

Please sign in to comment.