Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion] #5

Open
ryandesign opened this issue Oct 11, 2024 · 2 comments

Comments

@ryandesign
Copy link

umem 1.0.1 does not build with Apple clang 16 from Xcode 16 on macOS 15. The error is:

vmem_sbrk.c:137:14: error: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion]
  137 |   brk_result = brk((void *)new_brk);
      |              ^ ~~~~~~~~~~~~~~~~~~~~

This was reported to MacPorts here: https://trac.macports.org/ticket/71087

You define brk_result as an int:

int brk_result;

and assign the return value of brk to it:

brk_result = brk((void *)new_brk);

But brk is documented to return a void *, not an int.

You then compare the result to 0:

portableumem/vmem_sbrk.c

Lines 144 to 145 in 3fc772c

if (brk_result != 0)
return ((void *)-1);

But the manpage for brk says:

brk returns a pointer to the new end of memory if successful; otherwise -1 with errno set to indicate why the allocation failed.

so comparing to 0 does not seem correct.

@ryandesign
Copy link
Author

ryandesign commented Oct 11, 2024

But brk is documented to return a void *, not an int.

Huh. This appears to be a platform difference.

On macOS, brk returns a void *: the new end of memory if successful or -1 on failure.

However the Open Group says brk returns an int: 0 on success or -1 on failure.

@ryandesign
Copy link
Author

This is the fix I've added to MacPorts:

--- vmem_sbrk.c.orig	2009-03-05 18:56:48.000000000 -0600
+++ vmem_sbrk.c	2024-10-11 10:55:35.000000000 -0500
@@ -108,7 +108,7 @@
   uintptr_t ret_brk;
   uintptr_t high_brk;
   uintptr_t new_brk;
-  int brk_result;
+  intptr_t brk_result;
 
 #define ALIGNSZ   16
 #define BRKALIGN(x) (caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
@@ -134,9 +134,9 @@
     return ((void *)-1);
   }
 
-  brk_result = brk((void *)new_brk);
+  brk_result = (intptr_t)brk((void *)new_brk);
 
-  if (brk_result != 0)
+  if (brk_result == (intptr_t)-1)
     return ((void *)-1);
 
   if (actual_size != NULL)

Another option that I've seen some other programs use is to check brk's return type at configure time.

Finally, brk is obsolete and should not be used at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant