Skip to content

Commit

Permalink
check for type and unit allocation failures
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperthunk committed Feb 7, 2011
1 parent 022713f commit 7d3dc97
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -26,3 +26,4 @@ c_src/cscope.outcscope.out
*.swp
**/*.swp
inttest/deps/cspec
inttest/bin/all_specs
5 changes: 3 additions & 2 deletions c_src/erlxsl_internal.h
Expand Up @@ -359,9 +359,10 @@ free_task(XslTask *task) {
static void
free_command(Command *cmd) {
if (cmd != NULL) {
if (strcmp("transform", cmd->command_string) == 0) {
if (cmd->command_string != NULL && strcmp("transform", cmd->command_string) == 0) {
free_task(cmd->command_data.xsl_task);
} else {
} else {
// FIXME: we should be checking the DriverIOVec was actually assigned!
free_iov(cmd->command_data.iov);
}
free_iov(cmd->result);
Expand Down
26 changes: 26 additions & 0 deletions inttest/spec/ei.spec
Expand Up @@ -42,6 +42,32 @@ describe "Decoding Buffers using EI"

with_ei_fail(state = decode_ei_cmd(cmd, buf, &index));
state should be DecodeError;
free_command(cmd);
end

it "should return DecodeError when the initial type check returns an unexpected value"
char *buf = "12345";
int index = 0;
Command *cmd = ALLOC(sizeof(Command));
DriverState state;

with_type(ERL_FUN_EXT, state = decode_ei_cmd(cmd, buf, &index));
state should be DecodeError;
free_command(cmd);
end

it "should return OutOfMemory when allocation fails"
char *buf = "12345";
int index = 0;
int tuple_size = 2;
Command *cmd = ALLOC(sizeof(Command));
DriverState state;

with_tuple(tuple_size,
do_with_nomem(state = decode_ei_cmd(cmd, buf, &index))
);
state should be OutOfMemory;
free_command(cmd);
end

end
Expand Down
30 changes: 17 additions & 13 deletions inttest/spec/ei_test.h
Expand Up @@ -53,29 +53,35 @@

#define with_atom(s, expr) \
do { \
*test_type = ERL_ATOM_EXT; \
test_type = ERL_ATOM_EXT; \
test_buff = s; \
do { \
expr; \
} while (false)\
} while(false)

#define with_string(s, expr) \
do { \
*test_type = ERL_STRING_EXT; \
test_type = ERL_STRING_EXT; \
test_buff = s; \
do { \
expr; \
} while (false)\
} while(false)

#define with_tuple(size, expr) \
do { \
*test_type = ERL_SMALL_TUPLE_EXT; \
*test_arity = size; \
do { \
expr; \
} while (false)\
test_type = ERL_SMALL_TUPLE_EXT; \
test_arity = size; \
expr; \
} while(false)

#define with_type(type, expr) \
do { \
test_type = type; \
expr; \
} while (false)

#define with_ei_fail(expr) \
do { \
test_fail = true; \
Expand All @@ -89,25 +95,23 @@ static int ei_decode_atom(const char *buf, int *index, char *p);
static int ei_decode_tuple_header(const char *buf, int *index, int *arity);

static char *test_buff = NULL;
static int *test_type;
static int test_type;
static bool test_fail = false;
static int *test_arity;
static int test_arity;

static int ei_get_type(const char *buf, const int *index, int *type, int *size) {
// buf is ignored...
INFO("checking %i\n", test_fail);
if (test_fail == true) {
puts("failing");
return 1;
}
*type = *test_type;
*type = test_type;
return 0;
};

static int ei_decode_tuple_header(const char *buf, int *index, int *arity) {
// buf is ignored...
if (test_fail) return 1;
*arity= *test_arity;
*arity= test_arity;
(*index)++;
return 0;
};
Expand Down

0 comments on commit 7d3dc97

Please sign in to comment.