summaryrefslogtreecommitdiff
path: root/src/common/compress.c
diff options
context:
space:
mode:
authorAlexander Færøy <ahf@torproject.org>2017-04-18 03:39:43 +0200
committerNick Mathewson <nickm@torproject.org>2017-04-25 08:06:01 -0400
commitc171af048731cc00d77e17a1ff0be5c641024f8a (patch)
treee4e5fd4c89c8002e8ad3e1744516e43be154bdf5 /src/common/compress.c
parent300ac496853448eb3eeb3529d3aedaa8cc212ec3 (diff)
downloadtor-c171af048731cc00d77e17a1ff0be5c641024f8a.tar.gz
tor-c171af048731cc00d77e17a1ff0be5c641024f8a.zip
Use a switch-statement when checking for compression method.
This patch changes the way `tor_compress_new()`, `tor_compress_process()`, and `tor_compress_free()` handles different compression methods. This should give us compiler warnings in case an additional compression method is added, but the developer forgets to add handlers in the three aforementioned functions. See https://bugs.torproject.org/21663
Diffstat (limited to 'src/common/compress.c')
-rw-r--r--src/common/compress.c60
1 files changed, 46 insertions, 14 deletions
diff --git a/src/common/compress.c b/src/common/compress.c
index 74e235f006..4c87f292dd 100644
--- a/src/common/compress.c
+++ b/src/common/compress.c
@@ -149,14 +149,22 @@ tor_compress_new(int compress, compress_method_t method,
state = tor_malloc_zero(sizeof(tor_compress_state_t));
state->method = method;
- if (method == GZIP_METHOD || method == ZLIB_METHOD) {
- tor_zlib_compress_state_t *zlib_state =
- tor_zlib_compress_new(compress, method, compression_level);
+ switch (method) {
+ case GZIP_METHOD:
+ case ZLIB_METHOD: {
+ tor_zlib_compress_state_t *zlib_state =
+ tor_zlib_compress_new(compress, method, compression_level);
- if (zlib_state == NULL)
- goto err;
+ if (zlib_state == NULL)
+ goto err;
+
+ state->u.zlib_state = zlib_state;
+ break;
+ }
- state->u.zlib_state = zlib_state;
+ case NO_METHOD:
+ case UNKNOWN_METHOD:
+ goto err;
}
return state;
@@ -185,11 +193,19 @@ tor_compress_process(tor_compress_state_t *state,
{
tor_assert(state != NULL);
- if (state->method == GZIP_METHOD || state->method == ZLIB_METHOD)
- return tor_zlib_compress_process(state->u.zlib_state,
- out, out_len, in, in_len,
- finish);
+ switch (state->method) {
+ case GZIP_METHOD:
+ case ZLIB_METHOD:
+ return tor_zlib_compress_process(state->u.zlib_state,
+ out, out_len, in, in_len,
+ finish);
+ case NO_METHOD:
+ case UNKNOWN_METHOD:
+ goto err;
+ }
+
+ err:
return TOR_COMPRESS_ERROR;
}
@@ -200,8 +216,16 @@ tor_compress_free(tor_compress_state_t *state)
if (state == NULL)
return;
- if (state->method == GZIP_METHOD || state->method == ZLIB_METHOD)
- tor_zlib_compress_free(state->u.zlib_state);
+ switch (state->method) {
+ case GZIP_METHOD:
+ case ZLIB_METHOD:
+ tor_zlib_compress_free(state->u.zlib_state);
+ break;
+
+ case NO_METHOD:
+ case UNKNOWN_METHOD:
+ break;
+ }
tor_free(state);
}
@@ -212,9 +236,17 @@ tor_compress_state_size(const tor_compress_state_t *state)
{
tor_assert(state != NULL);
- if (state->method == GZIP_METHOD || state->method == ZLIB_METHOD)
- return tor_zlib_compress_state_size(state->u.zlib_state);
+ switch (state->method) {
+ case GZIP_METHOD:
+ case ZLIB_METHOD:
+ return tor_zlib_compress_state_size(state->u.zlib_state);
+
+ case NO_METHOD:
+ case UNKNOWN_METHOD:
+ goto err;
+ }
+ err:
return 0;
}