Skip to content

Commit

Permalink
[pmc] harmonize and combine out of bounds exception strings
Browse files Browse the repository at this point in the history
use less static string memory. no special context.
Use only "illegal argument" for unsupported negative arguments.
GH #1126

* index out of bounds,
* illegal argument (for illegal negative args),
* can't resize (for fixed*pmcs or size=0 arguments)
* StopIteration
  • Loading branch information
Reini Urban committed Nov 10, 2014
1 parent f898fa5 commit 7de5a35
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 171 deletions.
4 changes: 2 additions & 2 deletions src/hll.c
@@ -1,5 +1,5 @@
/*
Copyright (C) 2005-2011, Parrot Foundation.
Copyright (C) 2005-2014, Parrot Foundation.
=head1 NAME
Expand Down Expand Up @@ -357,7 +357,7 @@ Parrot_hll_get_HLL_type(PARROT_INTERP, INTVAL hll_id, INTVAL core_type)

if (core_type >= PARROT_MAX_CLASSES || core_type < 0) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedIntegerArray: index out of bounds!");
"index out of bounds");
}

type_array_attrs = PARROT_FIXEDINTEGERARRAY(type_array);
Expand Down
1 change: 0 additions & 1 deletion src/pmc/arrayiterator.pmc
Expand Up @@ -121,7 +121,6 @@ Marks the current idx/key and the aggregate as live.
PMC *array;

GET_ATTR_array(INTERP, SELF, array);

Parrot_gc_mark_PMC_alive(INTERP, array);
}

Expand Down
9 changes: 4 additions & 5 deletions src/pmc/bytebuffer.pmc
Expand Up @@ -90,7 +90,7 @@ Free the buffer when destroying.
unsigned char *content;
if (initial_size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("ByteBuffer: Cannot set buffer size to a negative number (%d)"), initial_size);
"illegal argument");
STATICSELF.init();
SET_ATTR_size(INTERP, SELF, initial_size);
SET_ATTR_allocated_size(INTERP, SELF, initial_size);
Expand Down Expand Up @@ -146,7 +146,7 @@ Resize the buffer to the given value.
unsigned char *content;
if (set_size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"Negative size in ByteBuffer");
"illegal argument");

GET_ATTR_allocated_size(INTERP, SELF, allocated_size);
if (set_size == 0) {
Expand Down Expand Up @@ -444,9 +444,8 @@ length in codepoints.
GET_ATTR_size(interp, SELF, size);

if (pos < 0 || pos > size)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_OUT_OF_BOUNDS,
"get_chars: index out of bounds");
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");

if (content == NULL) {
STRING *source;
Expand Down
9 changes: 4 additions & 5 deletions src/pmc/fixedbooleanarray.pmc
Expand Up @@ -91,7 +91,7 @@ Initializes the array.

if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedBooleanArray: Cannot set array size to a negative number (%d)"), size);
"illegal argument");

SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, size_in_bytes * BITS_PER_CHAR);
Expand Down Expand Up @@ -214,7 +214,7 @@ Returns the integer value of the element at index C<key>.

if (key < 0 || (UINTVAL)key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedBooleanArray: index out of bounds!");
"index out of bounds");

return (bit_array[key / BITS_PER_CHAR] & (1 << (key % BITS_PER_CHAR))) ? 1 : 0;
}
Expand Down Expand Up @@ -370,10 +370,9 @@ Resizes the array to C<size> elements.
unsigned char *bit_array;

GET_ATTR_size(INTERP, SELF, old_size);

if (old_size || size < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedBooleanArray: Can't resize!");
"Can't resize");

SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, size_in_bytes * BITS_PER_CHAR);
Expand All @@ -400,7 +399,7 @@ Sets the integer value of the element at index C<key> to C<value>.

if (key < 0 || (UINTVAL)key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedBooleanArray: index out of bounds!");
"index out of bounds");

if (value)
bit_array[key/BITS_PER_CHAR] |= (1 << (key % BITS_PER_CHAR));
Expand Down
10 changes: 5 additions & 5 deletions src/pmc/fixedfloatarray.pmc
Expand Up @@ -104,7 +104,8 @@ Initializes the array.
VTABLE void init_int(INTVAL size) {
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedFloatArray: Cannot set array size to a negative number (%d)"), size);
"illegal argument");

SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_float_array(INTERP, SELF, mem_gc_allocate_n_typed(INTERP, size, FLOATVAL));
PObj_custom_destroy_SET(SELF);
Expand Down Expand Up @@ -247,7 +248,7 @@ Returns the floating-point value of the element at index C<key>.
GET_ATTR_size(INTERP, SELF, size);
if (key < 0 || key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: index out of bounds!");
"index out of bounds");

GET_ATTR_float_array(INTERP, SELF, float_array);
return float_array[key];
Expand Down Expand Up @@ -365,7 +366,7 @@ Resizes the array to C<size> elements.
GET_ATTR_size(INTERP, SELF, old_size);
if (old_size || new_size < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: Can't resize!");
"Can't resize");

SET_ATTR_size(INTERP, SELF, new_size);
SET_ATTR_float_array(INTERP, SELF,
Expand Down Expand Up @@ -416,11 +417,10 @@ C<value>.
VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
FLOATVAL *float_array;
INTVAL size;

GET_ATTR_size(INTERP, SELF, size);
if (key < 0 || key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: index out of bounds!");
"index out of bounds");

GET_ATTR_float_array(INTERP, SELF, float_array);
float_array[key] = value;
Expand Down
14 changes: 4 additions & 10 deletions src/pmc/fixedintegerarray.pmc
Expand Up @@ -67,7 +67,7 @@ Initializes the array.
VTABLE void init_int(INTVAL size) {
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedIntegerArray: Cannot set array size to a negative number (%d)"), size);
"illegal argument");
if (size > 0) {
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_int_array(INTERP, SELF, mem_gc_allocate_n_typed(INTERP, size, INTVAL));
Expand Down Expand Up @@ -112,7 +112,6 @@ Creates and returns a copy of the array.

INTVAL * int_array;
PMC * const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);

GET_ATTR_int_array(INTERP, SELF, int_array);

if (int_array) {
Expand Down Expand Up @@ -194,12 +193,11 @@ Returns the integer value of the element at index C<key>.
VTABLE INTVAL get_integer_keyed_int(INTVAL key) :no_wb {
INTVAL *int_array;
INTVAL size;

GET_ATTR_size(INTERP, SELF, size);

if (key < 0 || key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedIntegerArray: index out of bounds!");
"index out of bounds");

GET_ATTR_int_array(INTERP, SELF, int_array);
return int_array[key];
Expand Down Expand Up @@ -288,7 +286,6 @@ Returns the Parrot string representation C<key>.
STRING *res = CONST_STRING(INTERP, "[ ");
INTVAL n;
INTVAL j;

GET_ATTR_size(INTERP, SELF, n);

for (j = 0; j < n; ++j) {
Expand Down Expand Up @@ -385,12 +382,11 @@ Resizes the array to C<size> elements.
VTABLE void set_integer_native(INTVAL size) {
INTVAL *int_array;
INTVAL cur_size;

GET_ATTR_size(INTERP, SELF, cur_size);

if (cur_size || size < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedIntegerArray: Can't resize!");
"Can't resize");

SET_ATTR_size(INTERP, SELF, size);
GET_ATTR_int_array(INTERP, SELF, int_array);
Expand All @@ -412,12 +408,11 @@ Sets the integer value of the element at index C<key> to C<value>.
VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
INTVAL *int_array;
INTVAL size;

GET_ATTR_size(INTERP, SELF, size);

if (key < 0 || key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedIntegerArray: index out of bounds!");
"index out of bounds");

GET_ATTR_int_array(INTERP, SELF, int_array);
int_array[key] = value;
Expand Down Expand Up @@ -544,7 +539,6 @@ The C<==> operation. Compares two array to hold equal elements.
return 0;

n = SELF.elements();

if (VTABLE_elements(INTERP, value) != n)
return 0;

Expand Down
26 changes: 11 additions & 15 deletions src/pmc/fixedpmcarray.pmc
Expand Up @@ -115,7 +115,7 @@ Initializes the array.
VTABLE void init_int(INTVAL size) :manual_wb {
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: Cannot set array size to a negative number (%d)"), size);
"illegal argument");

SELF.set_integer_native(size);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ Returns the PMC value of the element at index C<key>.

if (key < 0 || key >= PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: index out of bounds!"));
"index out of bounds");

data = PMC_array(SELF);
return data[key];
Expand Down Expand Up @@ -414,13 +414,12 @@ array.

if (PMC_size(SELF) && size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: Can't resize!"));
"Can't resize");
if (!size)
return;

if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: Cannot set array size to a negative number"));
"illegal argument");

PMC_size(SELF) = size;
data = mem_gc_allocate_n_typed(INTERP, size, PMC *);
Expand Down Expand Up @@ -572,7 +571,7 @@ Sets the PMC value of the element at index C<key> to C<*src>.

if (key < 0 || key >= PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: index out of bounds!"));
"index out of bounds");

data = PMC_array(SELF);
data[key] = src;
Expand Down Expand Up @@ -675,7 +674,7 @@ Returns TRUE is the element at C<key> exists; otherwise returns false.
PMC **data;
if (key < 0 || key >= PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedPMCArray: index out of bounds!"));
"index out of bounds");

data = PMC_array(SELF);
return !PMC_IS_NULL(data[key]);
Expand Down Expand Up @@ -707,16 +706,13 @@ of this array.
/* start from end? */
if (offset < 0)
offset += elems0;
if (offset < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal splice offset"));
if (count < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("illegal argument"));
if (count + offset > PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
_("index out of bounds"));

"illegal argument");
if (offset < 0 || count + offset > PMC_size(SELF))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");
/* TODO: use faster memmove as in ResizablePMCArray */
for (count--; count >= 0; --count) {
VTABLE_set_pmc_keyed_int(INTERP, SELF, offset + count, value);
}
Expand Down
11 changes: 4 additions & 7 deletions src/pmc/fixedstringarray.pmc
Expand Up @@ -47,7 +47,7 @@ Initializes the array.
VTABLE void init_int(INTVAL size) :manual_wb {
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
_("FixedStringArray: Cannot set array size to a negative number (%d)"), size);
"illegal argument");
if (size) {
SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_str_array(INTERP,
Expand All @@ -69,11 +69,9 @@ Destroys the array.
*/

VTABLE void destroy() :no_wb {

STRING **str_array;

GET_ATTR_str_array(INTERP, SELF, str_array);

if (str_array)
mem_gc_free(INTERP, str_array);
}
Expand Down Expand Up @@ -293,7 +291,7 @@ Returns the Parrot string value of the element at index C<key>.
GET_ATTR_size(INTERP, SELF, size);
if (key < 0 || (UINTVAL)key >= size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedStringArray: index out of bounds!");
"index out of bounds");

GET_ATTR_str_array(INTERP, SELF, str_array);
return str_array[key];
Expand Down Expand Up @@ -367,7 +365,7 @@ cause an exception to be thrown.

if (old_size || new_size < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedStringArray: Can't resize!");
"Can't resize");

SET_ATTR_size(INTERP, SELF, new_size);
SET_ATTR_str_array(INTERP, SELF,
Expand Down Expand Up @@ -453,10 +451,9 @@ Sets the Parrot string value of the element at index C<key> to C<value>.

if (key < 0 || key >= SELF.elements())
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedStringArray: index out of bounds!");
"index out of bounds");

GET_ATTR_str_array(INTERP, SELF, str_array);

str_array[key] = value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pmc/hashiterator.pmc
Expand Up @@ -277,7 +277,7 @@ the next one.

if (attrs->elements < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"StopIteration");
"StopIteration");

ret = Parrot_pmc_new(INTERP, enum_class_HashIteratorKey);
VTABLE_set_pointer_keyed_int(INTERP, ret, 0, attrs->parrot_hash);
Expand Down Expand Up @@ -315,7 +315,7 @@ the next one.

if (attrs->elements < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"StopIteration");
"StopIteration");

return Parrot_hash_key_to_int(INTERP, attrs->parrot_hash, attrs->bucket->key);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pmc/integer.pmc
Expand Up @@ -1279,7 +1279,7 @@ and 36, inclusive.

if ((base < 2) || (base > 36))
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"get_as_base: base out of bounds");
"base out of bounds");

result = Parrot_str_from_int_base(INTERP, buf,
(HUGEINTVAL)VTABLE_get_integer(INTERP, SELF),
Expand Down Expand Up @@ -1348,7 +1348,7 @@ Set to a random value.
}
if (a == b || a > b) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"set_random: range start must be less than range end (%d, %d)", a, b);
"range start must be less than range end (%d, %d)", a, b);
}
{
const double spread = (double)(b - a + 1);
Expand Down

0 comments on commit 7de5a35

Please sign in to comment.