Ticket #4233: 0001-Explicitly-prevent-requests-for-zero-memory.patch

File 0001-Explicitly-prevent-requests-for-zero-memory.patch, 3.4 KB (added by mansour, 8 years ago)
  • configure.in

    From f733acf5ae44b6a0f63d9c702c9adf9fa424a1b4 Mon Sep 17 00:00:00 2001
    From: Mansour Moufid <mansourmoufid@gmail.com>
    Date: Thu, 13 Oct 2011 16:45:40 -0400
    Subject: [PATCH 1/3] Explicitly prevent requests for zero memory.
    
    Make the `tor_malloc' memory allocation function in `util.c'
    always fail when given a zero size parameter.
    
    Remove the `MALLOC_ZERO_WORKS' macro and related tests
    in `configure.in'.
    ---
     configure.in      |   24 ------------------------
     src/common/util.c |   12 +++---------
     src/common/util.h |    4 ++--
     3 files changed, 5 insertions(+), 35 deletions(-)
    
    diff --git a/configure.in b/configure.in
    index d861093..1cbc077 100644
    a b if test "$tor_cv_null_is_zero" != no; then 
    872872            [Define to 1 iff memset(0) sets pointers to NULL])
    873873fi
    874874
    875 # And what happens when we malloc zero?
    876 AC_CACHE_CHECK([whether we can malloc(0) safely.], tor_cv_malloc_zero_works,
    877 [AC_RUN_IFELSE([AC_LANG_SOURCE(
    878 [[#include <stdlib.h>
    879 #include <string.h>
    880 #include <stdio.h>
    881 #ifdef HAVE_STDDEF_H
    882 #include <stddef.h>
    883 #endif
    884 int main () { return malloc(0)?0:1; }]])],
    885        [tor_cv_malloc_zero_works=yes],
    886        [tor_cv_malloc_zero_works=no],
    887        [tor_cv_malloc_zero_works=cross])])
    888 
    889 if test "$tor_cv_malloc_zero_works" = cross; then
    890   # Cross-compiling; let's hope that the target isn't raving mad.
    891   AC_MSG_NOTICE([Cross-compiling: we'll assume that we need to check malloc() arguments for 0.])
    892 fi
    893 
    894 if test "$tor_cv_malloc_zero_works" = yes; then
    895   AC_DEFINE([MALLOC_ZERO_WORKS], 1,
    896             [Define to 1 iff malloc(0) returns a pointer])
    897 fi
    898 
    899875# whether we seem to be in a 2s-complement world.
    900876AC_CACHE_CHECK([whether we are using 2s-complement arithmetic], tor_cv_twos_complement,
    901877[AC_RUN_IFELSE([AC_LANG_SOURCE(
  • src/common/util.c

    diff --git a/src/common/util.c b/src/common/util.c
    index 79e09e4..b8e76bd 100644
    a b  
    125125 * ignored otherwise.
    126126 */
    127127void *
    128 _tor_malloc(size_t size DMALLOC_PARAMS)
     128_tor_malloc(const size_t size DMALLOC_PARAMS)
    129129{
    130130  void *result;
    131131
     132  tor_assert(size != 0);
    132133  tor_assert(size < SIZE_T_CEILING);
    133134
    134 #ifndef MALLOC_ZERO_WORKS
    135   /* Some libc mallocs don't work when size==0. Override them. */
    136   if (size==0) {
    137     size=1;
    138   }
    139 #endif
    140 
    141135#ifdef USE_DMALLOC
    142136  result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
    143137#else
    _tor_malloc(size_t size DMALLOC_PARAMS) 
    159153 * the process on error.  (Same as calloc(size,1), but never returns NULL.)
    160154 */
    161155void *
    162 _tor_malloc_zero(size_t size DMALLOC_PARAMS)
     156_tor_malloc_zero(const size_t size DMALLOC_PARAMS)
    163157{
    164158  /* You may ask yourself, "wouldn't it be smart to use calloc instead of
    165159   * malloc+memset?  Perhaps libc's calloc knows some nifty optimization trick
  • src/common/util.h

    diff --git a/src/common/util.h b/src/common/util.h
    index 77ed1ca..9e2fd45 100644
    a b  
    7070#define tor_fragile_assert()
    7171
    7272/* Memory management */
    73 void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
    74 void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
     73void *_tor_malloc(const size_t size DMALLOC_PARAMS) ATTR_MALLOC;
     74void *_tor_malloc_zero(const size_t size DMALLOC_PARAMS) ATTR_MALLOC;
    7575void *_tor_malloc_roundup(size_t *size DMALLOC_PARAMS) ATTR_MALLOC;
    7676void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
    7777char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));