[or-cvs] r17490: {tor} Simplify mmap object layout to avoid confusing static analys (tor/trunk/src/common)

nickm at seul.org nickm at seul.org
Fri Dec 5 02:17:41 UTC 2008


Author: nickm
Date: 2008-12-04 21:17:41 -0500 (Thu, 04 Dec 2008)
New Revision: 17490

Modified:
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
Log:
Simplify mmap object layout to avoid confusing static analysis tools, and us too.

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2008-12-05 02:17:34 UTC (rev 17489)
+++ tor/trunk/src/common/compat.c	2008-12-05 02:17:41 UTC (rev 17490)
@@ -111,14 +111,6 @@
 #endif
 
 #ifdef HAVE_SYS_MMAN_H
-/** Implementation for tor_mmap_t: holds the regular tor_mmap_t, along
- * with extra fields needed for mmap()-based memory mapping. */
-typedef struct tor_mmap_impl_t {
-  tor_mmap_t base;
-  size_t mapping_size; /**< Size of the actual mapping. (This is this file
-                        * size, rounded up to the nearest page.) */
-} tor_mmap_impl_t;
-
 /** Try to create a memory mapping for <b>filename</b> and return it.  On
  * failure, return NULL.  Sets errno properly, using ERANGE to mean
  * "empty file". */
@@ -128,7 +120,7 @@
   int fd; /* router file */
   char *string;
   int page_size;
-  tor_mmap_impl_t *res;
+  tor_mmap_t *res;
   size_t size, filesize;
 
   tor_assert(filename);
@@ -168,33 +160,25 @@
     return NULL;
   }
 
-  res = tor_malloc_zero(sizeof(tor_mmap_impl_t));
-  res->base.data = string;
-  res->base.size = filesize;
+  res = tor_malloc_zero(sizeof(tor_mmap_t));
+  res->data = string;
+  res->size = filesize;
   res->mapping_size = size;
 
-  return &(res->base);
+  return res;
 }
 /** Release storage held for a memory mapping. */
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  tor_mmap_impl_t *h = SUBTYPE_P(handle, tor_mmap_impl_t, base);
-  munmap((char*)h->base.data, h->mapping_size);
-  tor_free(h);
+  munmap((char*)handle->data, handle->mapping_size);
+  tor_free(handle);
 }
 #elif defined(MS_WINDOWS)
-/** Implementation for tor_mmap_t: holds the regular tor_mmap_t, along
- * with extra fields needed for WIN32 memory mapping. */
-typedef struct win_mmap_t {
-  tor_mmap_t base;
-  HANDLE file_handle;
-  HANDLE mmap_handle;
-} win_mmap_t;
 tor_mmap_t *
 tor_mmap_file(const char *filename)
 {
-  win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
+  tor_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
   int empty = 0;
   res->file_handle = INVALID_HANDLE_VALUE;
   res->mmap_handle = NULL;
@@ -209,9 +193,9 @@
   if (res->file_handle == INVALID_HANDLE_VALUE)
     goto win_err;
 
-  res->base.size = GetFileSize(res->file_handle, NULL);
+  res->size = GetFileSize(res->file_handle, NULL);
 
-  if (res->base.size == 0) {
+  if (res->size == 0) {
     log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
     empty = 1;
     goto err;
@@ -229,13 +213,13 @@
                                        NULL);
   if (res->mmap_handle == NULL)
     goto win_err;
-  res->base.data = (char*) MapViewOfFile(res->mmap_handle,
-                                         FILE_MAP_READ,
-                                         0, 0, 0);
-  if (!res->base.data)
+  res->data = (char*) MapViewOfFile(res->mmap_handle,
+                                    FILE_MAP_READ,
+                                    0, 0, 0);
+  if (!res->data)
     goto win_err;
 
-  return &(res->base);
+  return res;
  win_err: {
     DWORD e = GetLastError();
     int severity = (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND) ?
@@ -251,23 +235,22 @@
  err:
   if (empty)
     errno = ERANGE;
-  tor_munmap_file(&res->base);
+  tor_munmap_file(res);
   return NULL;
 }
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  win_mmap_t *h = SUBTYPE_P(handle, win_mmap_t, base);
   if (handle->data)
     /* This is an ugly cast, but without it, "data" in struct tor_mmap_t would
        have to be redefined as non-const. */
     UnmapViewOfFile( (LPVOID) handle->data);
 
-  if (h->mmap_handle != NULL)
+  if (handle->mmap_handle != NULL)
     CloseHandle(h->mmap_handle);
-  if (h->file_handle != INVALID_HANDLE_VALUE)
+  if (handle->file_handle != INVALID_HANDLE_VALUE)
     CloseHandle(h->file_handle);
-  tor_free(h);
+  tor_free(handle);
 }
 #else
 tor_mmap_t *

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2008-12-05 02:17:34 UTC (rev 17489)
+++ tor/trunk/src/common/compat.h	2008-12-05 02:17:41 UTC (rev 17490)
@@ -220,6 +220,16 @@
 typedef struct tor_mmap_t {
   const char *data; /**< Mapping of the file's contents. */
   size_t size; /**< Size of the file. */
+
+  /* None of the fields below should be accessed from outside compat.c */
+#ifdef HAVE_SYS_MMAN_H
+  size_t mapping_size; /**< Size of the actual mapping. (This is this file
+                        * size, rounded up to the nearest page.) */
+#elif defined MS_WINDOWS
+  HANDLE file_handle;
+  HANDLE mmap_handle;
+#endif
+
 } tor_mmap_t;
 
 tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));



More information about the tor-commits mailing list