From 038830ff1cd0b816acdbe0548927d80587f2bdd8 Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Tue, 14 May 2013 11:14:58 +0200 Subject: [PATCH] compression_wrapper: Add asserts. --- src/compression_wrapper.c | 42 +++++++++++++++++++++++++----------------- src/compression_wrapper.h | 6 ++++-- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/compression_wrapper.c b/src/compression_wrapper.c index 0ce0587..8285150 100644 --- a/src/compression_wrapper.c +++ b/src/compression_wrapper.c @@ -216,6 +216,9 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) CR_FILE *file = NULL; cr_CompressionType type; + assert(mode < CR_CW_MODE_SENTINEL); + assert(comtype < CR_CW_COMPRESSION_SENTINEL); + if (!filename || (mode != CR_CW_MODE_READ && mode != CR_CW_MODE_WRITE)) { g_debug("%s: Filename is NULL or bad mode value", __func__); return NULL; @@ -475,14 +478,15 @@ cr_close(CR_FILE *cr_file) int cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) { - if (!cr_file || !buffer || cr_file->mode != CR_CW_MODE_READ) { - return CR_CW_ERR; - } - - int bzerror; int ret; + assert(cr_file); + assert(buffer); + + if (cr_file->mode != CR_CW_MODE_READ) + return CR_CW_ERR; + switch (cr_file->type) { case (CR_CW_NO_COMPRESSION): // ---------------------------------------- @@ -560,9 +564,11 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) int bzerror; int ret = CR_CW_ERR; - if (!cr_file || !buffer || cr_file->mode != CR_CW_MODE_WRITE) { + assert(cr_file); + assert(buffer); + + if (cr_file->mode != CR_CW_MODE_WRITE) return ret; - } switch (cr_file->type) { @@ -631,15 +637,15 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) int cr_puts(CR_FILE *cr_file, const char *str) { - if (!cr_file || !str || cr_file->mode != CR_CW_MODE_WRITE) { - return CR_CW_ERR; - } - - size_t len; int bzerror; int ret = CR_CW_ERR; + assert(cr_file); + + if (!str || cr_file->mode != CR_CW_MODE_WRITE) + return CR_CW_ERR; + switch (cr_file->type) { case (CR_CW_NO_COMPRESSION): // ---------------------------------------- @@ -686,16 +692,18 @@ cr_puts(CR_FILE *cr_file, const char *str) int cr_printf(CR_FILE *cr_file, const char *format, ...) { - if (!cr_file || !format || cr_file->mode != CR_CW_MODE_WRITE) { + va_list vl; + int ret; + gchar *buf = NULL; + + assert(cr_file); + + if (!format || cr_file->mode != CR_CW_MODE_WRITE) return CR_CW_ERR; - } - va_list vl; va_start(vl, format); // Fill format string - int ret; - gchar *buf = NULL; ret = g_vasprintf(&buf, format, vl); va_end(vl); diff --git a/src/compression_wrapper.h b/src/compression_wrapper.h index 3b451dd..710a858 100644 --- a/src/compression_wrapper.h +++ b/src/compression_wrapper.h @@ -38,13 +38,15 @@ typedef enum { CR_CW_GZ_COMPRESSION, /*!< Gzip compression */ CR_CW_BZ2_COMPRESSION, /*!< BZip2 compression */ CR_CW_XZ_COMPRESSION, /*!< XZ compression */ + CR_CW_COMPRESSION_SENTINEL, /*!< Sentinel of the list */ } cr_CompressionType; /** Open modes. */ typedef enum { - CR_CW_MODE_READ, /*!< Read mode */ - CR_CW_MODE_WRITE /*!< Write mode */ + CR_CW_MODE_READ, /*!< Read mode */ + CR_CW_MODE_WRITE, /*!< Write mode */ + CR_CW_MODE_SENTINEL, /*!< Sentinel of the list */ } cr_OpenMode; -- 2.7.4