bug in mmap() and mremap() return value - and a fix

Massimiliano Ghilardi massimiliano.ghilardi at gmail.com
Thu Jan 9 00:27:42 UTC 2014


> In conclusion, I like your ideas and I updated the patch by integrating
> most of them.
>

Except that I messed up the build order (headbang).
Fixed patch in attachment
-------------- next part --------------
diff --git a/posix/early.lisp b/posix/early.lisp
index 88167d7..4f5af24 100644
--- a/posix/early.lisp
+++ b/posix/early.lisp
@@ -119,6 +119,18 @@
    (object :initarg :object :reader errno-object)
    (function-name :initarg :function-name :reader function-name)))
 
+;; NOTE: this is an ugly type-punning. An alternative is to compute
+;; this value from (cffi:foreign-type-size :uintptr), but it requires
+;; also knowing how many bits are in a byte.
+(defconstant +most-positive-uintptr+ (with-foreign-object (p :intptr)
+                                       (setf (mem-ref p :intptr) -1)
+                                       (mem-ref p :uintptr)))
+
+;; on some systems, map-failed is groveled as -1 :(
+#-windows
+(defvar *map-failed-pointer* (make-pointer (logand map-failed +most-positive-uintptr+)))
+
+
 ;;; FIXME: undocumented in cffi-grovel.
 (defun make-from-pointer-function-name (type-name)
   (format-symbol t "~A-~A-~A-~A" '#:make type-name '#:from '#:pointer))
diff --git a/posix/wrappers.lisp b/posix/wrappers.lisp
index ae49ad8..dfc2a15 100644
--- a/posix/wrappers.lisp
+++ b/posix/wrappers.lisp
@@ -60,9 +60,8 @@
 
 #-windows
 (defwrapper "mmap" ("void*" (errno-wrapper
-                             :long
-                             :error-predicate (lambda (p) (= p map-failed))
-                             :return-filter make-pointer))
+                             :pointer
+                             :error-predicate (lambda (p) (pointer-eq p *map-failed-pointer*))))
   (start :pointer)
   (length ("size_t" size))
   (prot :int)
@@ -72,9 +71,8 @@
 
 #+linux
 (defwrapper "mremap" ("void*" (errno-wrapper
-                               :long
-                               :error-predicate (lambda (p) (= p map-failed))
-                               :return-filter make-pointer))
+                               :pointer
+                               :error-predicate (lambda (p) (pointer-eq p *map-failed-pointer*))))
   (old-address :pointer)
   (old-size ("size_t" size))
   (new-size ("size_t" size))


More information about the osicat-devel mailing list