Commit efccdd42 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added sanity check for lengths in encoder

parent 89cfad68
......@@ -29,6 +29,8 @@
//define the following to disable encoding of typedefs
#undef LABCOMM_WITHOUT_TYPE_DEFS
#undef LABCOMM2014_WITH_SANITY_CHECKS
struct encoder {
struct labcomm2014_encoder encoder;
LABCOMM_SIGNATURE_ARRAY_DEF(registered, int);
......@@ -36,10 +38,47 @@ struct encoder {
LABCOMM_SIGNATURE_ARRAY_DEF(typedefs, int);
};
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
static int expectedByteCount;
static void encoder_check_write_start(struct labcomm2014_encoder *e, int numBytes)
{
int previouslyWritten = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &previouslyWritten);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
expectedByteCount = numBytes + previouslyWritten;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("previously written: %d bytes, length = %d bytes, expected = %d bytes\n",
previouslyWritten, numBytes, expectedByteCount);
#endif
}
static int encoder_check_write_end(struct labcomm2014_encoder *e)
{
int written = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &written);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
int result = 0;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("DEBUG: encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
#endif
if(written != expectedByteCount) {
printf("WARNING! encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
result = -EINVAL;
}
return result;
}
#endif
/* XXX: TEMPORARY PLACEHOLDERS FOR INTENTIONS */
static int TODO_sizeof_intentions(const struct labcomm2014_signature *signature) {
return labcomm2014_size_string(signature->name) + 2;
int res = labcomm2014_size_string(signature->name) + 2;
return res;
}
static int TODO_encode_intentions(
......@@ -301,19 +340,30 @@ static int internal_reg_type(
if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; }
printf("internal_reg_type: %s\n", signature->name);
int sig_size = calc_sig_encoded_size(e, signature);
int len = (labcomm2014_size_packed32(index) +
TODO_sizeof_intentions(signature) +
labcomm2014_size_packed32(signature->size) +
sig_size);
int len1= labcomm2014_size_packed32(index);
int len2 =TODO_sizeof_intentions(signature);
int len3 =labcomm2014_size_packed32(signature->size);
int len4 =sig_size;
printf("len (index) : %d. (intentions) : %d, (sig_size): %d, (sig): %d)\n",
len1, len2, len3, len4);
int len = len1 + len2 + len3 + len4;
labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm2014_write_packed32(e->writer, len);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_start(e, len);
#endif
labcomm2014_write_packed32(e->writer, index);
TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, LABCOMM2014_FALSE);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_end(e);
#endif
labcomm2014_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
......
......@@ -26,9 +26,14 @@
#include <stdarg.h>
#include "labcomm2014_private.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_ioctl.h"
#define BUFFER_SIZE 2048
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int bytecount_carry = 0;
#endif
struct labcomm2014_fd_writer {
struct labcomm2014_writer writer;
struct labcomm2014_writer_action_context action_context;
......@@ -82,6 +87,9 @@ static int fd_start(struct labcomm2014_writer *w,
const struct labcomm2014_signature *signature,
void *value)
{
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0;
return w->error;
......@@ -107,18 +115,45 @@ static int fd_flush(struct labcomm2014_writer *w,
} else if (err == 0) {
w->error = -EINVAL;
}
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0;
return w->error;
}
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int fd_ioctl(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int signature_index,
const struct labcomm2014_signature *signature,
uint32_t action, va_list arg)
{
int result = -ENOTSUP;
switch (action) {
case LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN: {
int *value = va_arg(arg, int*);
*value = w->pos + bytecount_carry;
result = 0;
} break;
}
return result;
}
#endif
static const struct labcomm2014_writer_action action = {
.alloc = fd_alloc,
.free = fd_free,
.start = fd_start,
.end = fd_flush,
.flush = fd_flush,
#ifdef LABCOMM_WITH_SANITY_CHECKS
.ioctl = fd_ioctl
#else
.ioctl = NULL
#endif
};
struct labcomm2014_writer *labcomm2014_fd_writer_new(struct labcomm2014_memory *memory,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment