Linux console tools development discussion
 help / color / mirror / Atom feed
From: Michael Forney <mforney@mforney.org>
To: kbd@lists.altlinux.org
Subject: [kbd] [PATCH] libkeymap: Avoid pointer arithmetic on `void *`
Date: Wed, 26 Jun 2019 20:07:19 -0700
Message-ID: <20190627030719.4116-1-mforney@mforney.org> (raw)

ISO C requires that the pointer operand to the binary + operator be to
a complete object type[0]. Since we are working with byte sizes, use
`char *` instead.

[0] http://port70.net/~nsz/c/c11/n1570.html#6.5.6p2

Signed-off-by: Michael Forney <mforney@mforney.org>
---
Another option with less casting is to change the type of the `array`
member of `struct lk_array` from `void *` to `char *`. Let me know
if you would prefer this approach:

https://github.com/michaelforney/kbd/commit/5554deb1df4263d5d6b782a484dbb78b1b3ad569

 configure.ac          |  1 +
 src/libkeymap/array.c | 12 ++++++------
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/configure.ac b/configure.ac
index f2389b2..991d95e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -88,6 +88,7 @@ CC_CHECK_CFLAGS_APPEND([\
 	-Wmissing-format-attribute \
 	-Wmissing-noreturn \
 	-Wmissing-prototypes \
+	-Wpointer-arith \
 	-Wredundant-decls \
 	-Wshadow \
 	-Wstrict-prototypes \
diff --git a/src/libkeymap/array.c b/src/libkeymap/array.c
index 0583c13..b2fb6b6 100644
--- a/src/libkeymap/array.c
+++ b/src/libkeymap/array.c
@@ -67,7 +67,7 @@ lk_array_exists(struct lk_array *a, ssize_t i)
 		return 0;
 	}
 
-	s = (char *)(a->array + (a->memb * i));
+	s = (char *) a->array + (a->memb * i);
 
 	for (k = 0; k < a->memb; k++) {
 		if (s[k] != 0)
@@ -84,7 +84,7 @@ lk_array_get(struct lk_array *a, ssize_t i)
 		errno = EINVAL;
 		return NULL;
 	}
-	return a->array + (a->memb * i);
+	return (char *) a->array + (a->memb * i);
 }
 
 void *
@@ -114,7 +114,7 @@ array_resize(struct lk_array *a, ssize_t i)
 			return -ENOMEM;
 		}
 
-		memset(tmp + (a->memb * a->total), 0, (size_t) (a->memb * (i + 1 - a->total)));
+		memset((char *) tmp + (a->memb * a->total), 0, (size_t) (a->memb * (i + 1 - a->total)));
 
 		a->array = tmp;
 		a->total = i + 1;
@@ -130,7 +130,7 @@ lk_array_set(struct lk_array *a, ssize_t i, const void *e)
 	if (ret < 0)
 		return ret;
 
-	memcpy(a->array + (a->memb * i), e, (size_t) a->memb);
+	memcpy((char *) a->array + (a->memb * i), e, (size_t) a->memb);
 	a->count++;
 
 	return 0;
@@ -145,7 +145,7 @@ lk_array_unset(struct lk_array *a, ssize_t i)
 	}
 
 	if (lk_array_exists(a, i)) {
-		memset(a->array + (a->memb * i), 0, (size_t) a->memb);
+		memset((char *) a->array + (a->memb * i), 0, (size_t) a->memb);
 		a->count--;
 	}
 
@@ -160,7 +160,7 @@ lk_array_append(struct lk_array *a, const void *e)
 	if (ret < 0)
 		return ret;
 
-	memcpy(a->array + (a->memb * a->count), e, (size_t) a->memb);
+	memcpy((char *) a->array + (a->memb * a->count), e, (size_t) a->memb);
 	a->count++;
 
 	return 0;
-- 
2.20.1



             reply	other threads:[~2019-06-27  3:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27  3:07 Michael Forney [this message]
2019-06-27  9:17 ` Alexey Gladkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190627030719.4116-1-mforney@mforney.org \
    --to=mforney@mforney.org \
    --cc=kbd@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux console tools development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/kbd/0 kbd/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kbd kbd/ http://lore.altlinux.org/kbd \
		kbd@lists.altlinux.org kbd@lists.altlinux.ru kbd@lists.altlinux.com
	public-inbox-index kbd

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.kbd


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git