[commit: ghc] wip/erikd/remove-nat: RtsUtils: Use `size_t` instead of `int` where appropriate (2b86f6f)

git at git.haskell.org git at git.haskell.org
Tue May 10 23:59:14 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/erikd/remove-nat
Link       : http://ghc.haskell.org/trac/ghc/changeset/2b86f6f078408d5857091b6bc653da28aaa554f7/ghc

>---------------------------------------------------------------

commit 2b86f6f078408d5857091b6bc653da28aaa554f7
Author: Erik de Castro Lopo <erikd at mega-nerd.com>
Date:   Mon May 2 20:07:05 2016 +1000

    RtsUtils: Use `size_t` instead of `int` where appropriate
    
    Functions like `stgMallocBytes` take a size parameter which was of type
    `int`, but is commonly used as `stgMallocBytes (sizeof (...))`. This is
    problematic because the `sizeof` operator returns `size_t` so that on 64 bit
    systems, in this common use case the `size_t` parameter would be truncated to
    32 bits when passed to `stgMallocBytes` where it was cast back to `size_t`.


>---------------------------------------------------------------

2b86f6f078408d5857091b6bc653da28aaa554f7
 rts/Linker.c   |  6 +++---
 rts/RtsUtils.c | 22 +++++++++-------------
 rts/RtsUtils.h |  6 +++---
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/rts/Linker.c b/rts/Linker.c
index 981df25..109d17d 100644
--- a/rts/Linker.c
+++ b/rts/Linker.c
@@ -3799,9 +3799,9 @@ ocVerifyImage_PEi386 ( ObjectCode* oc )
    IF_DEBUG(linker, i=1);
    if (i == 0) return 1;
 
-   debugBelch( "sectab offset = %" FMT_Int "\n", ((UChar*)sectab) - ((UChar*)hdr) );
-   debugBelch( "symtab offset = %" FMT_Int "\n", ((UChar*)symtab) - ((UChar*)hdr) );
-   debugBelch( "strtab offset = %" FMT_Int "\n", ((UChar*)strtab) - ((UChar*)hdr) );
+   debugBelch( "sectab offset = %" FMT_SizeT "\n", ((UChar*)sectab) - ((UChar*)hdr) );
+   debugBelch( "symtab offset = %" FMT_SizeT "\n", ((UChar*)symtab) - ((UChar*)hdr) );
+   debugBelch( "strtab offset = %" FMT_SizeT "\n", ((UChar*)strtab) - ((UChar*)hdr) );
 
    debugBelch("\n" );
    debugBelch( "Machine:           0x%x\n", (UInt32)(hdr->Machine) );
diff --git a/rts/RtsUtils.c b/rts/RtsUtils.c
index c837143..716d203 100644
--- a/rts/RtsUtils.c
+++ b/rts/RtsUtils.c
@@ -57,13 +57,11 @@ extern char *ctime_r(const time_t *, char *);
    -------------------------------------------------------------------------- */
 
 void *
-stgMallocBytes (int n, char *msg)
+stgMallocBytes (size_t n, char *msg)
 {
-    char *space;
-    size_t n2;
+    void *space;
 
-    n2 = (size_t) n;
-    if ((space = (char *) malloc(n2)) == NULL) {
+    if ((space = malloc(n)) == NULL) {
       /* Quoting POSIX.1-2008 (which says more or less the same as ISO C99):
        *
        *   "Upon successful completion with size not equal to 0, malloc() shall
@@ -85,13 +83,11 @@ stgMallocBytes (int n, char *msg)
 }
 
 void *
-stgReallocBytes (void *p, int n, char *msg)
+stgReallocBytes (void *p, size_t n, char *msg)
 {
-    char *space;
-    size_t n2;
+    void *space;
 
-    n2 = (size_t) n;
-    if ((space = (char *) realloc(p, (size_t) n2)) == NULL) {
+    if ((space = realloc(p, n)) == NULL) {
       /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
       rtsConfig.mallocFailHook((W_) n, msg); /*msg*/
       stg_exit(EXIT_INTERNAL_ERROR);
@@ -100,11 +96,11 @@ stgReallocBytes (void *p, int n, char *msg)
 }
 
 void *
-stgCallocBytes (int n, int m, char *msg)
+stgCallocBytes (size_t n, size_t m, char *msg)
 {
-    char *space;
+    void *space;
 
-    if ((space = (char *) calloc((size_t) n, (size_t) m)) == NULL) {
+    if ((space = calloc(n, m)) == NULL) {
       /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
       rtsConfig.mallocFailHook((W_) n*m, msg); /*msg*/
       stg_exit(EXIT_INTERNAL_ERROR);
diff --git a/rts/RtsUtils.h b/rts/RtsUtils.h
index d76c921..2d5e5de 100644
--- a/rts/RtsUtils.h
+++ b/rts/RtsUtils.h
@@ -18,12 +18,12 @@
 void initAllocator(void);
 void shutdownAllocator(void);
 
-void *stgMallocBytes(int n, char *msg)
+void *stgMallocBytes(size_t n, char *msg)
     GNUC3_ATTRIBUTE(__malloc__);
 
-void *stgReallocBytes(void *p, int n, char *msg);
+void *stgReallocBytes(void *p, size_t n, char *msg);
 
-void *stgCallocBytes(int n, int m, char *msg)
+void *stgCallocBytes(size_t n, size_t m, char *msg)
      GNUC3_ATTRIBUTE(__malloc__);
 
 char *stgStrndup(const char *s, size_t n);



More information about the ghc-commits mailing list