Ticket #4233: 0003-An-example-use-of-the-tor_sum_sizet-function.patch

File 0003-An-example-use-of-the-tor_sum_sizet-function.patch, 1.9 KB (added by mansour, 8 years ago)
  • src/or/routerparse.c

    From e9eeeac570a871d1c86652ae9caaece1c420679a Mon Sep 17 00:00:00 2001
    From: Mansour Moufid <mansourmoufid@gmail.com>
    Date: Thu, 13 Oct 2011 17:31:15 -0400
    Subject: [PATCH 3/3] An example use of the `tor_sum_sizet' function.
    
    Use the `tor_sum_sizet' function to safely compute a length variable
    (itself used to allocate memory) in the function
    `router_parse_entry_from_string' of the file `src/or/routerparse.c'.
    
    Note that the subsequent ``len--'' is exact since the value of `len'
    is guaranteed to be as expected (i.e. at least 1 as 1 was a summand;
    otherwise `len' is set to 0 and `tor_malloc' would have failed).
    
    The following would have been more explicit but redundant:
    
        tor_assert(tor_sum_sizet(&len, 3,
                     router->cache_info.signed_descriptor_len,
                     router->cache_info.annotations_len,
                     1) == 3);
    ---
     src/or/routerparse.c |   12 ++++++++----
     1 files changed, 8 insertions(+), 4 deletions(-)
    
    diff --git a/src/or/routerparse.c b/src/or/routerparse.c
    index 6fd8db2..b16ea2b 100644
    a b router_parse_entry_from_string(const char *s, const char *end, 
    13601360  router->cache_info.annotations_len = s-start_of_annotations + prepend_len;
    13611361  router->cache_info.signed_descriptor_len = end-s;
    13621362  if (cache_copy) {
    1363     size_t len = router->cache_info.signed_descriptor_len +
    1364                  router->cache_info.annotations_len;
    1365     char *cp =
    1366       router->cache_info.signed_descriptor_body = tor_malloc(len+1);
     1363    char *cp = NULL;
     1364    size_t len;
     1365    tor_sum_sizet(&len, 3,
     1366                  router->cache_info.signed_descriptor_len,
     1367                  router->cache_info.annotations_len,
     1368                  1);
     1369    cp = router->cache_info.signed_descriptor_body = tor_malloc(len);
     1370    len--;
    13671371    if (prepend_annotations) {
    13681372      memcpy(cp, prepend_annotations, prepend_len);
    13691373      cp += prepend_len;