Skip to content

#81 Fix casting pointer to different size integer#23

Merged
ooxi merged 1 commit intogerbv:mainfrom
ooxi:patch/81-fix-casting-pointer-to-different-size-integer
Sep 16, 2021
Merged

#81 Fix casting pointer to different size integer#23
ooxi merged 1 commit intogerbv:mainfrom
ooxi:patch/81-fix-casting-pointer-to-different-size-integer

Conversation

@ooxi
Copy link
Copy Markdown
Contributor

@ooxi ooxi commented Sep 16, 2021

src/scheme.c has the problem of casting pointers to int of different sizes. This problem becomes apparent in 64-bit compilers.

But changed uintptr_t to size_t as discussed in Pull Request #20 by @efa

See SourceForge patch #81 by Hiroshi Yoshikawa

--- a/src/scheme.c	2020-10-22 07:12:44.000000000 +0900
+++ b/src/scheme.c	2021-08-03 22:15:56.721406100 +0900
@@ -568,7 +568,7 @@ static int alloc_cellseg(scheme *sc, int
      char *cp;
      long i;
      int k;
-     unsigned int adj=ADJ;
+     uintptr_t adj=ADJ;
 
      if(adj<sizeof(struct cell)) {
        adj=sizeof(struct cell);
@@ -583,8 +583,8 @@ static int alloc_cellseg(scheme *sc, int
 	  i = ++sc->last_cell_seg ;
 	  sc->alloc_seg[i] = cp;
 	  /* adjust in TYPE_BITS-bit boundary */
-	  if((unsigned long)cp%adj!=0) {
-	    cp=(char*)(adj*((unsigned long)cp/adj+1));
+	  if((uintptr_t)cp%adj!=0) {
+	    cp=(char*)(adj*((uintptr_t)cp/adj+1));
 	  }
         /* insert new segment in address order */
 	  newp=(pointer)cp;

But changed 'uintptr_t' to 'size_t' as opening a .gvp file on Windows always crash

I re-tested also on Linux

https://sourceforge.net/p/gerbv/patches/81/
@ooxi ooxi marked this pull request as ready for review September 16, 2021 08:51
@ooxi ooxi merged commit e4b344e into gerbv:main Sep 16, 2021
@ooxi ooxi deleted the patch/81-fix-casting-pointer-to-different-size-integer branch September 16, 2021 08:51
@eyal0
Copy link
Copy Markdown
Collaborator

eyal0 commented Sep 17, 2021

If you are worried about a cast from one type to another type of a different size, the best protection is to have the compiler enforce it.

static_assert(sizeof(uintptr_t) == sizeof(cp))

This solution will catch bugs on all architectures, even ones that we haven't encountered yet. It's nice when we have have the computer find our bugs for us automatically.

@efa
Copy link
Copy Markdown
Contributor

efa commented Sep 17, 2021

to me seems a good idea. Please add a PR for this

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

Successfully merging this pull request may close these issues.

3 participants