Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added assertions to check for programming errors.

Mostly they just make sure that function arguments are reasonable.

There are also some that check rs_buf's invariants in buffer.c. There
will be similar assertions for any additional data structures that are
needed.
  • Loading branch information...
commit 9911d5c4714fcfe9f1aab3d144a85437c3d2f24c 1 parent 17afa9f
@ingramj authored
Showing with 51 additions and 25 deletions.
  1. +29 −21 buffer.c
  2. +7 −2 error.c
  3. +9 −0 read.c
  4. +2 −2 rescheme.h
  5. +4 −0 write.c
View
50 buffer.c
@@ -1,63 +1,71 @@
#include "rescheme.h"
+#include <assert.h>
+
#define _RS_BUF_GROWBY 128
void rs_buf_init(struct rs_buf *buf)
{
- if (buf != NULL) {
- buf->buf = NULL;
- buf->capacity = 0;
- buf->offset = 0;
- }
+ assert(buf != NULL);
+
+ buf->buf = NULL;
+ buf->cap = 0;
+ buf->off = 0;
}
void rs_buf_reset(struct rs_buf *buf)
{
- if (buf != NULL) {
- free(buf->buf);
- buf->capacity = 0;
- buf->offset = 0;
- }
+ assert(buf != NULL);
+
+ free(buf->buf);
+ buf->buf = NULL;
+ buf->cap = 0;
+ buf->off = 0;
}
struct rs_buf *rs_buf_push(struct rs_buf *buf, char c)
{
- if (buf == NULL) {
- errno = EINVAL;
- return NULL;
- }
+ assert(buf != NULL);
+ assert((buf->buf == NULL && buf->cap == 0 && buf->off == 0) ||
+ (buf->buf != NULL && buf->cap > 0 && buf->off < buf->cap));
if (buf->buf == NULL) {
buf->buf = calloc(_RS_BUF_GROWBY, 1);
if (buf->buf == NULL) {
return NULL;
}
- buf->capacity = _RS_BUF_GROWBY;
- buf->offset = 0;
+ buf->cap = _RS_BUF_GROWBY;
}
- if (buf->offset >= buf->capacity - 1) {
- char *newbuf = realloc(buf->buf, buf->capacity + _RS_BUF_GROWBY);
+ if (buf->off >= buf->cap - 1) {
+ char *newbuf = realloc(buf->buf, buf->cap + _RS_BUF_GROWBY);
if (newbuf == NULL) {
return NULL;
}
buf->buf = newbuf;
- buf->capacity += _RS_BUF_GROWBY;
+ buf->cap += _RS_BUF_GROWBY;
}
- buf->buf[buf->offset++] = c;
+ buf->buf[buf->off++] = c;
+ assert(buf->buf != NULL);
+ assert(buf->cap > 0);
+ assert(buf->off > 0 && buf->off < buf->cap);
return buf;
}
const char *rs_buf_str(struct rs_buf *buf)
{
+ assert(buf != NULL);
+ assert((buf->buf == NULL && buf->cap == 0 && buf->off == 0) ||
+ (buf->buf != NULL && buf->cap > 0 && buf->off < buf->cap));
+
if (buf->buf == NULL) {
return "";
}
- buf->buf[buf->offset] = '\0';
+ buf->buf[buf->off] = '\0';
return (const char *) buf->buf;
}
View
9 error.c
@@ -1,5 +1,6 @@
#include "rescheme.h"
+#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
@@ -10,12 +11,15 @@
void eprintf(int status, char const * const func, char const * const fmt, ...)
{
+ assert(func != NULL);
+ assert(fmt != NULL);
+
int olderr = errno;
char buf[BUFSIZE];
int bytes;
va_list argv;
- (void) fflush(stderr);
+ (void) fflush(stdout);
bytes = snprintf(buf, BUFSIZE, "%s: ", func);
@@ -30,7 +34,8 @@ void eprintf(int status, char const * const func, char const * const fmt, ...)
fprintf(stderr, "%s\n", buf);
if (bytes >= BUFSIZE - 1) {
- fprintf(stderr, "warning: error message from %s has been truncated.\n", func);
+ fprintf(stderr, "warning: error message from %s has been truncated.\n",
+ func);
}
errno = olderr;
View
9 read.c
@@ -1,4 +1,5 @@
#include "rescheme.h"
+
#include <assert.h>
#include <ctype.h>
#include <limits.h>
@@ -60,6 +61,8 @@ static void get_word(struct rs_buf *buf, FILE *in, int c, int n);
rs_object rs_read(FILE *in)
{
+ assert(in != NULL);
+
int c;
struct rs_buf buf;
rs_object obj = rs_eof;
@@ -316,6 +319,9 @@ rs_object rs_read(FILE *in)
static inline rs_object check_num(struct rs_buf *buf, int base)
{
+ assert(buf != NULL);
+ assert(base == 10 || base == 2 || base == 8 || base == 16);
+
long value = strtol(rs_buf_str(buf), NULL, base);
if (value < rs_fixnum_min || value > rs_fixnum_max) {
rs_fatal("number out of range");
@@ -326,6 +332,9 @@ static inline rs_object check_num(struct rs_buf *buf, int base)
static void get_word(struct rs_buf *buf, FILE *in, int c, int n)
{
+ assert(buf != NULL);
+ assert(in != NULL);
+
BUF_PUSH(buf, tolower(c));
int loop = 1;
while (loop) {
View
4 rescheme.h
@@ -161,8 +161,8 @@ const char *rs_buf_str(struct rs_buf *buf);
struct rs_buf {
char *buf;
- size_t offset;
- size_t capacity;
+ size_t off;
+ size_t cap;
};
void eprintf(int status, char const * const func, char const * const fmt, ...);
View
4 write.c
@@ -1,8 +1,12 @@
#include "rescheme.h"
+#include <assert.h>
+
int rs_write(FILE *out, rs_object obj)
{
+ assert(out != NULL);
+
int result;
if (rs_fixnum_p(obj)) {
result = fprintf(out, "%ld", (long)rs_fixnum_value(obj));
Please sign in to comment.
Something went wrong with that request. Please try again.