diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-12-04 01:08:26 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-12-04 01:08:26 -0500 |
commit | 03ebf05e4f7646d4574acbc952b23976e4f8a175 (patch) | |
tree | b6dee89777ebfe3658bc091e4fd74682c042a5f9 /common | |
parent | 4477953aae80e03f881042d476ecc95d6b24ab5d (diff) |
get strict (-Wall -Werror -Wextra), clean up
Diffstat (limited to 'common')
-rw-r--r-- | common/nslcd-prot.h | 299 |
1 files changed, 168 insertions, 131 deletions
diff --git a/common/nslcd-prot.h b/common/nslcd-prot.h index acc42f6..5355bfa 100644 --- a/common/nslcd-prot.h +++ b/common/nslcd-prot.h @@ -41,16 +41,22 @@ pass -DDEBUG_PROT to do overall protocol debugging, and -DDEBUG_PROT_DUMP to dump the actual bytestream. */ +#if GCC_VERSION(3, 0) +static void removedcall() __attribute__((unused)); +#endif +static void removedcall() {} + + #ifdef DEBUG_PROT /* define a debugging macro to output logging */ #include <string.h> #include <errno.h> #define DEBUG_PRINT(fmt, arg) \ - fprintf(stderr, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, \ - __PRETTY_FUNCTION__, arg); + fprintf(stderr, "%s:%d:%s: " fmt "\n", \ + __FILE__, __LINE__, __PRETTY_FUNCTION__, arg) #else /* DEBUG_PROT */ /* define an empty debug macro to disable logging */ -#define DEBUG_PRINT(fmt, arg) +#define DEBUG_PRINT(fmt, arg) removedcall() #endif /* not DEBUG_PROT */ #ifdef DEBUG_PROT_DUMP @@ -66,11 +72,13 @@ static void debug_dump(const void *ptr, size_t size) fprintf(stderr, "\n"); } #define DEBUG_DUMP(ptr, size) \ - fprintf(stderr, "%s:%d:%s:", __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - debug_dump(ptr, size); + do { \ + fprintf(stderr, "%s:%d:%s:", __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + debug_dump(ptr, size); \ + } while(0) #else /* DEBUG_PROT_DUMP */ /* define an empty debug macro to disable logging */ -#define DEBUG_DUMP(ptr, size) +#define DEBUG_DUMP(ptr, size) removedcall() #endif /* not DEBUG_PROT_DUMP */ @@ -78,39 +86,45 @@ static void debug_dump(const void *ptr, size_t size) call the ERROR_OUT_WRITEERROR macro these macros may require the availability of the following variables: - int32_t tmpint32; - temporary variable */ #define WRITE(fp, ptr, size) \ - DEBUG_PRINT("WRITE : var="__STRING(ptr)" size=%d", (int)size); \ - DEBUG_DUMP(ptr, size); \ - if (tio_write(fp, ptr, (size_t)size)) \ - { \ - DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s", \ - strerror(errno)); \ - ERROR_OUT_WRITEERROR(fp); \ - } + do { \ + DEBUG_PRINT("WRITE : var="__STRING(ptr)" size=%d", (int)size); \ + DEBUG_DUMP(ptr, size); \ + if (tio_write(fp, ptr, (size_t)size)) \ + { \ + DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s", \ + strerror(errno)); \ + ERROR_OUT_WRITEERROR(fp); \ + } \ + } while(0) #define WRITE_INT32(fp, i) \ - DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%08x", (int)i); \ - tmpint32 = htonl((int32_t)(i)); \ - WRITE(fp, &tmpint32, sizeof(int32_t)) + ({ \ + DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%08x", (int)i); \ + int32_t tmpint32 = htonl((int32_t)(i)); \ + WRITE(fp, &tmpint32, sizeof(int32_t)); \ + tmpint32; \ + }) #define WRITE_STRING(fp, str) \ - DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"", (str)); \ - if ((str) == NULL) \ - { \ - WRITE_INT32(fp, 0); \ - } \ - else \ - { \ - WRITE_INT32(fp, strlen(str)); \ - tmpint32 = ntohl(tmpint32); \ - if (tmpint32 > 0) \ + ({ \ + DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"", (str)); \ + if ((str) == NULL) \ { \ - WRITE(fp, (str), tmpint32); \ + WRITE_INT32(fp, 0); \ } \ - } + else \ + { \ + int32_t tmpint32 = WRITE_INT32(fp, strlen(str)); \ + tmpint32 = ntohl(tmpint32); \ + if (tmpint32 > 0) \ + { \ + WRITE(fp, (str), tmpint32); \ + } \ + } \ + }) #define WRITE_STRINGLIST(fp, arr) \ if ((arr) == NULL) \ @@ -155,52 +169,57 @@ static void debug_dump(const void *ptr, size_t size) call the ERROR_OUT_READERROR or ERROR_OUT_BUFERROR macro these macros may require the availability of the following variables: - int32_t tmpint32; - temporary variable */ #define READ(fp, ptr, size) \ - if (tio_read(fp, ptr, (size_t)size)) \ - { \ - DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s", \ - strerror(errno)); \ - ERROR_OUT_READERROR(fp); \ - } \ - DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d", (int)(size)); \ - DEBUG_DUMP(ptr, size); + do { \ + if (tio_read(fp, ptr, (size_t)size)) \ + { \ + DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s", \ + strerror(errno)); \ + ERROR_OUT_READERROR(fp); \ + } \ + DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d", (int)(size)); \ + DEBUG_DUMP(ptr, size); \ + } while(0) #define READ_INT32(fp, i) \ - READ(fp, &tmpint32, sizeof(int32_t)); \ - (i) = (int32_t)ntohl(tmpint32); \ - DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32==%08x", (int)(i)); + ({ \ + int32_t tmpint32; \ + READ(fp, &tmpint32, sizeof(int32_t)); \ + (i) = (int32_t)ntohl(tmpint32); \ + DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32==%08x", (int)(i)); \ + }) /* read a string in a fixed-size "normal" buffer */ #define READ_STRING(fp, buffer) \ - /* read the size of the string */ \ - READ(fp, &tmpint32, sizeof(int32_t)); \ - tmpint32 = ntohl(tmpint32); \ - DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d", tmpint32); \ - /* check if read would fit */ \ - if (((size_t)tmpint32) >= sizeof(buffer)) \ - { \ - /* will not fit */ \ - tmpint32 = (tmpint32 - sizeof(buffer)) + 1; \ - DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \ - ERROR_OUT_BUFERROR(fp); \ - } \ - /* read string from the stream */ \ - if (tmpint32 > 0) \ - { \ - READ(fp, buffer, (size_t)tmpint32); \ - } \ - /* null-terminate string in buffer */ \ - buffer[tmpint32] = '\0'; \ - DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"", buffer); - + ({ \ + /* read the size of the string */ \ + int32_t tmpint32; \ + READ(fp, &tmpint32, sizeof(int32_t)); \ + tmpint32 = ntohl(tmpint32); \ + DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d", tmpint32); \ + /* check if read would fit */ \ + if (((size_t)tmpint32) >= sizeof(buffer)) \ + { \ + /* will not fit */ \ + tmpint32 = (tmpint32 - sizeof(buffer)) + 1; \ + DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \ + ERROR_OUT_BUFERROR(fp); \ + } \ + /* read string from the stream */ \ + if (tmpint32 > 0) \ + { \ + READ(fp, buffer, (size_t)tmpint32); \ + } \ + /* null-terminate string in buffer */ \ + buffer[tmpint32] = '\0'; \ + DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"", buffer); \ + }) /* READ BUF macros that read data into a pre-allocated buffer. these macros may require the availability of the following variables: - int32_t tmpint32; - temporary variable char *buffer; - pointer to a buffer for reading strings size_t buflen; - the size of the buffer size_t bufptr; - the current position in the buffer @@ -212,17 +231,19 @@ static void debug_dump(const void *ptr, size_t size) /* check that the buffer has sz bytes left in it */ #define BUF_CHECK(fp, sz) \ - if ((bufptr + (size_t)(sz)) > buflen) \ - { \ - /* will not fit */ \ - tmpint32 = bufptr + (sz) - (buflen); \ - DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \ - ERROR_OUT_BUFERROR(fp); \ - } + ({ \ + if ((bufptr + (size_t)(sz)) > buflen) \ + { \ + /* will not fit */ \ + int32_t tmpint32 = bufptr + (sz) - (buflen); \ + DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \ + ERROR_OUT_BUFERROR(fp); \ + } \ + }) /* move the buffer pointer */ #define BUF_SKIP(sz) \ - bufptr += (size_t)(sz); + bufptr += (size_t)(sz) /* move BUF_CUR foreward so that it is aligned to the specified type width */ @@ -257,74 +278,87 @@ static void debug_dump(const void *ptr, size_t size) /* read string in the buffer (using buffer, buflen and bufptr) and store the actual location of the string in field */ #define READ_BUF_STRING(fp, field) \ - /* read the size of the string */ \ - READ(fp, &tmpint32, sizeof(int32_t)); \ - tmpint32 = ntohl(tmpint32); \ - DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" strlen=%d", tmpint32); \ - /* check if read would fit */ \ - BUF_CHECK(fp, tmpint32 + 1); \ - /* read string from the stream */ \ - if (tmpint32 > 0) \ - { \ - READ(fp, BUF_CUR, (size_t)tmpint32); \ - } \ - /* null-terminate string in buffer */ \ - BUF_CUR[tmpint32] = '\0'; \ - DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" string=\"%s\"", BUF_CUR); \ - /* prepare result */ \ - (field) = BUF_CUR; \ - BUF_SKIP(tmpint32 + 1); + ({ \ + int32 tmpint32; \ + /* read the size of the string */ \ + READ(fp, &tmpint32, sizeof(int32_t)); \ + tmpint32 = ntohl(tmpint32); \ + DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" strlen=%d", tmpint32); \ + /* check if read would fit */ \ + BUF_CHECK(fp, tmpint32 + 1); \ + /* read string from the stream */ \ + if (tmpint32 > 0) \ + { \ + READ(fp, BUF_CUR, (size_t)tmpint32); \ + } \ + /* null-terminate string in buffer */ \ + BUF_CUR[tmpint32] = '\0'; \ + DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" string=\"%s\"", BUF_CUR); \ + /* prepare result */ \ + (field) = BUF_CUR; \ + BUF_SKIP(tmpint32 + 1); \ + }) /* read an array from a stram and store it as a null-terminated array list (size for the array is allocated) */ #define READ_BUF_STRINGLIST(fp, arr) \ - /* read the number of entries */ \ - READ(fp, &tmp3int32, sizeof(int32_t)); \ - tmp3int32 = ntohl(tmp3int32); \ - DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \ - /* allocate room for *char[num + 1] */ \ - BUF_ALLOC(fp, arr, char *, tmp3int32 + 1); \ - /* read all entries */ \ - for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \ - { \ - READ_BUF_STRING(fp, (arr)[tmp2int32]); \ - } \ - /* set last entry to NULL */ \ - (arr)[tmp2int32] = NULL; - + ({ \ + int32 tmpint32; \ + /* read the number of entries */ \ + READ(fp, &tmp3int32, sizeof(int32_t)); \ + tmp3int32 = ntohl(tmp3int32); \ + DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \ + /* allocate room for *char[num + 1] */ \ + BUF_ALLOC(fp, arr, char *, tmp3int32 + 1); \ + /* read all entries */ \ + for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \ + { \ + READ_BUF_STRING(fp, (arr)[tmp2int32]); \ + } \ + /* set last entry to NULL */ \ + (arr)[tmp2int32] = NULL; \ + }) /* SKIP macros for skipping over certain parts of the protocol stream. */ /* skip a number of bytes foreward */ #define SKIP(fp, sz) \ - DEBUG_PRINT("READ : skip %d bytes", (int)(sz)); \ - /* read (skip) the specified number of bytes */ \ - if (tio_skip(fp, sz)) \ - { \ - DEBUG_PRINT("READ : skip error: %s", strerror(errno)); \ - ERROR_OUT_READERROR(fp); \ - } + do { \ + DEBUG_PRINT("READ : skip %d bytes", (int)(sz)); \ + /* read (skip) the specified number of bytes */ \ + if (tio_skip(fp, sz)) \ + { \ + DEBUG_PRINT("READ : skip error: %s", strerror(errno)); \ + ERROR_OUT_READERROR(fp); \ + } \ + } while(0) /* read a string from the stream but don't do anything with the result */ #define SKIP_STRING(fp) \ - /* read the size of the string */ \ - READ(fp, &tmpint32, sizeof(int32_t)); \ - tmpint32 = ntohl(tmpint32); \ - DEBUG_PRINT("READ_STRING: skip %d bytes", (int)tmpint32); \ - /* read (skip) the specified number of bytes */ \ - SKIP(fp, tmpint32); + ({ \ + int32_t tmpint32; \ + /* read the size of the string */ \ + READ(fp, &tmpint32, sizeof(int32_t)); \ + tmpint32 = ntohl(tmpint32); \ + DEBUG_PRINT("READ_STRING: skip %d bytes", (int)tmpint32); \ + /* read (skip) the specified number of bytes */ \ + SKIP(fp, tmpint32); \ + }) /* skip a list of strings */ #define SKIP_STRINGLIST(fp) \ - /* read the number of entries */ \ - READ(fp, &tmp3int32, sizeof(int32_t)); \ - tmp3int32 = ntohl(tmp3int32); \ - DEBUG_PRINT("READ_STRLST: skip %d strings", (int)tmp3int32); \ - /* read all entries */ \ - for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \ - { \ - SKIP_STRING(fp); \ - } + ({ \ + int32_t tmpint32; \ + /* read the number of entries */ \ + READ(fp, &tmp3int32, sizeof(int32_t)); \ + tmp3int32 = ntohl(tmp3int32); \ + DEBUG_PRINT("READ_STRLST: skip %d strings", (int)tmp3int32); \ + /* read all entries */ \ + for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \ + { \ + SKIP_STRING(fp); \ + } \ + }) /* These are functions and macors for performing common operations in @@ -333,11 +367,14 @@ static void debug_dump(const void *ptr, size_t size) /* Read the response code (the result code of the query) from the stream. */ #define READ_RESPONSE_CODE(fp) \ - READ(fp, &tmpint32, sizeof(int32_t)); \ - tmpint32 = ntohl(tmpint32); \ - if (tmpint32 != (int32_t)NSLCD_RESULT_BEGIN) \ - { \ - ERROR_OUT_NOSUCCESS(fp); \ - } + ({ \ + int32_t tmpint32; \ + READ(fp, &tmpint32, sizeof(int32_t)); \ + tmpint32 = ntohl(tmpint32); \ + if (tmpint32 != (int32_t)NSLCD_RESULT_BEGIN) \ + { \ + ERROR_OUT_NOSUCCESS(fp); \ + } \ + }) #endif /* not COMMON__NSLCD_PROT_H */ |