Patchwork [v2,02/11] curl: change magic number to macro

login
register
mail settings
Submitter Fam Zheng
Date May 14, 2013, 2:26 a.m.
Message ID <1368498390-20738-3-git-send-email-famz@redhat.com>
Download mbox | patch
Permalink /patch/243571/
State New
Headers show

Comments

Fam Zheng - May 14, 2013, 2:26 a.m.
String field length is duplicated in two places. Make it a macro.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/curl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Stefan Hajnoczi - May 14, 2013, 8:04 a.m.
On Tue, May 14, 2013 at 10:26:21AM +0800, Fam Zheng wrote:
> @@ -70,7 +70,8 @@ typedef struct CURLState
>      size_t buf_start;
>      size_t buf_off;
>      size_t buf_len;
> -    char range[128];
> +#define CURL_RANGE_SIZE 128
> +    char range[CURL_RANGE_SIZE];
>      char errmsg[CURL_ERROR_SIZE];
>      char in_use;
>  } CURLState;
> @@ -567,7 +568,7 @@ static void curl_readv_bh_cb(void *p)
>      state->orig_buf = g_malloc(state->buf_len);
>      state->acb[0] = acb;
>  
> -    snprintf(state->range, 127, "%zd-%zd", start, end);
> +    snprintf(state->range, CURL_RANGE_SIZE - 1, "%zd-%zd", start, end);

Simpler solution: sizeof(state->range) - 1

Then you don't need to introduce "CURL_RANGE_SIZE" which risks
conflicting with libcurl's namespace.

Patch

diff --git a/block/curl.c b/block/curl.c
index 14f4552..aa78086 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -70,7 +70,8 @@  typedef struct CURLState
     size_t buf_start;
     size_t buf_off;
     size_t buf_len;
-    char range[128];
+#define CURL_RANGE_SIZE 128
+    char range[CURL_RANGE_SIZE];
     char errmsg[CURL_ERROR_SIZE];
     char in_use;
 } CURLState;
@@ -567,7 +568,7 @@  static void curl_readv_bh_cb(void *p)
     state->orig_buf = g_malloc(state->buf_len);
     state->acb[0] = acb;
 
-    snprintf(state->range, 127, "%zd-%zd", start, end);
+    snprintf(state->range, CURL_RANGE_SIZE - 1, "%zd-%zd", start, end);
     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);