You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by jo...@apache.org on 2023/01/23 16:47:04 UTC

svn commit: r1906947 - /apr/apr/trunk/shmem/unix/shm.c

Author: jorton
Date: Mon Jan 23 16:47:04 2023
New Revision: 1906947

URL: http://svn.apache.org/viewvc?rev=1906947&view=rev
Log:
* shmem/unix/shm.c (apr_shm_open):
  Use ftruncate() directly for the shm_open() path avoiding
  unnecessary complexity - and lseek() - of using apr_file_t wrapper.
  Also catch mmap() errors.
    
PR: 66435
Github: closes #38

Modified:
    apr/apr/trunk/shmem/unix/shm.c

Modified: apr/apr/trunk/shmem/unix/shm.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/shmem/unix/shm.c?rev=1906947&r1=1906946&r2=1906947&view=diff
==============================================================================
--- apr/apr/trunk/shmem/unix/shm.c (original)
+++ apr/apr/trunk/shmem/unix/shm.c Mon Jan 23 16:47:04 2023
@@ -161,7 +161,7 @@ APR_DECLARE(apr_status_t) apr_shm_create
     apr_size_t nbytes;
 #endif
 #if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \
-    APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
+    APR_USE_SHMEM_MMAP_TMP
     apr_file_t *file;   /* file where metadata is stored */
 #endif
 
@@ -325,25 +325,22 @@ APR_DECLARE(apr_status_t) apr_shm_create
             return errno;
         }
 
-        status = apr_os_file_put(&file, &tmpfd,
-                                 APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
-                                 pool);
-        if (status != APR_SUCCESS) {
-            return status;
-        }
-
-        status = apr_file_trunc(file, new_m->realsize);
+        /* Note that apr_file_trunc() also calls lseek() so wrapping
+         * the fd into an apr_file_t and doing this indirectly is
+         * undesirable, see PR 66435. */
+        status = ftruncate(tmpfd, new_m->realsize) < 0 ? errno : APR_SUCCESS;
         if (status != APR_SUCCESS && status != APR_ESPIPE) {
             shm_unlink(shm_name); /* we're failing, remove the object */
+            close(tmpfd);
             return status;
         }
         new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
                            MAP_SHARED, tmpfd, 0);
-
-        /* FIXME: check for errors */
-
-        status = apr_file_close(file);
-        if (status != APR_SUCCESS) {
+        status = (new_m->base == (void *)-1) ? errno : APR_SUCCESS;
+        /* fd no longer needed once the memory is mapped. */
+        close(tmpfd);
+        if (status) {
+            shm_unlink(shm_name);
             return status;
         }
 #endif /* APR_USE_SHMEM_MMAP_SHM */