[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Use a switch-statement when checking for compression method.



commit c171af048731cc00d77e17a1ff0be5c641024f8a
Author: Alexander Færøy <ahf@xxxxxxxxxxxxxx>
Date:   Tue Apr 18 03:39:43 2017 +0200

    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
---
 src/common/compress.c | 60 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 46 insertions(+), 14 deletions(-)

diff --git a/src/common/compress.c b/src/common/compress.c
index 74e235f..4c87f29 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;
 }
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits