lib: fix pointer of type 'void *' used in arithmetic
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"
Just remove these by calculating on "char *" where 1-byte data size is assumed
--- include/sbi/sbi_scratch.h | 4 ++-- lib/sbi/sbi_fifo.c | 6 +++--- lib/sbi/sbi_string.c | 4 ++-- lib/utils/irqchip/plic.c | 8 ++++---- lib/utils/timer/aclint_mtimer.c | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-)
include/sbi/sbi_scratch.h +2 -2
@@ -104,11 +104,11 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size);void sbi_scratch_free_offset (unsigned long offset);/** Get pointer from offset in sbi_scratch */ -#define sbi_scratch_offset_ptr (scratch, offset) ((void *)scratch + (offset)) +#define sbi_scratch_offset_ptr (scratch, offset) (void *)((char *)(scratch) + (offset))/** Get pointer from offset in sbi_scratch for current HART */ #define sbi_scratch_thishart_offset_ptr (offset) \ - ((void *)sbi_scratch_thishart_ptr() + (offset)) + (void *)((char *)sbi_scratch_thishart_ptr() + (offset))/** HART id to scratch table */ extern struct sbi_scratch *hartid_to_scratch_table[];
lib/sbi/sbi_fifo.c +3 -3
@@ -66,7 +66,7 @@ static inline void __sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data)if (head >= fifo->num_entries) head = head - fifo->num_entries; -sbi_memcpy (fifo->queue + head * fifo->entry_size, data, fifo->entry_size); +sbi_memcpy ((char *)fifo->queue + head * fifo->entry_size, data, fifo->entry_size); fifo->avail++; }
@@ -142,7 +142,7 @@ int sbi_fifo_inplace_update(struct sbi_fifo *fifo, void *in, index = fifo->tail + i;if (index >= fifo->num_entries) index -= fifo->num_entries; - entry = (void *)fifo->queue + (u32 )index * fifo->entry_size; + entry = (char *)fifo->queue + (u32 )index * fifo->entry_size; ret =fptr (in, entry);if (ret ==SBI_FIFO_SKIP || ret ==SBI_FIFO_UPDATED ) {
@@ -184,7 +184,7 @@ int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data)return SBI_ENOENT ; } -sbi_memcpy (data, fifo->queue + (u32 )fifo->tail * fifo->entry_size, +sbi_memcpy (data, (char *)fifo->queue + (u32 )fifo->tail * fifo->entry_size, fifo->entry_size); fifo->avail--;
lib/sbi/sbi_string.c +2 -2
@@ -149,8 +149,8 @@ void *sbi_memmove(void *dest, const void *src, size_t count) count--; } }else { - temp1 = dest + count -1 ; - temp2 = src + count -1 ; + temp1 = (char *)dest + count -1 ; + temp2 = (char *)src + count -1 ;while (count >0 ) { *temp1-- = *temp2--;
lib/utils/irqchip/plic.c +4 -4
@@ -23,7 +23,7 @@static void plic_set_priority (struct plic_data *plic,u32 source,u32 val) { -volatile void *plic_priority = (void *)plic->addr + +volatile void *plic_priority = (char *)plic->addr +PLIC_PRIORITY_BASE +4 * source;writel (val, plic_priority); }
@@ -35,19 +35,19 @@ void plic_set_thresh(struct plic_data *plic, u32 cntxid, u32 val)if (!plic)return ; - plic_thresh = (void *)plic->addr + + plic_thresh = (char *)plic->addr +PLIC_CONTEXT_BASE +PLIC_CONTEXT_STRIDE * cntxid;writel (val, plic_thresh); }void plic_set_ie (struct plic_data *plic,u32 cntxid,u32 word_index,u32 val) { -volatile void *plic_ie; +volatile char *plic_ie;if (!plic)return ; - plic_ie = (void *)plic->addr + + plic_ie = (char *)plic->addr +PLIC_ENABLE_BASE +PLIC_ENABLE_STRIDE * cntxid;writel (val, plic_ie + word_index *4 ); }
lib/utils/timer/aclint_mtimer.c +1 -1
@@ -47,7 +47,7 @@ static u64 mtimer_time_rd32(volatile u64 *addr)static void mtimer_time_wr32 (bool timecmp,u64 value,volatile u64 *addr) {writel_relaxed ((timecmp) ?-1U :0U , (void *)(addr)); -writel_relaxed ((u32)(value >>32 ), (void *)(addr) +0x04 ); +writel_relaxed ((u32)(value >>32 ), (char *)(addr) +0x04 );writel_relaxed ((u32 )value, (void *)(addr)); }
Signature · 1 line
2.25.1
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
In reply to Jukka Laitinen[Re-sending this since my emails end up being empty...]
Quoted 4 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)
I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
Quoted 123 lines
Signed-off-by Jukka Laitinen <[email protected]>--- include/sbi/sbi_scratch.h | 4 ++-- lib/sbi/sbi_fifo.c | 6 +++--- lib/sbi/sbi_string.c | 4 ++-- lib/utils/irqchip/plic.c | 8 ++++---- lib/utils/timer/aclint_mtimer.c | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-)include/sbi/sbi_scratch.h +2
-2@@ -104,11 +104,11 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size);void sbi_scratch_free_offset (unsigned long offset);/** Get pointer from offset in sbi_scratch */ -#define sbi_scratch_offset_ptr (scratch, offset) ((void *)scratch + (offset)) +#define sbi_scratch_offset_ptr (scratch, offset) (void *)((char *)(scratch) + (offset))/** Get pointer from offset in sbi_scratch for current HART */ #define sbi_scratch_thishart_offset_ptr (offset) \ - ((void *)sbi_scratch_thishart_ptr() + (offset)) + (void *)((char *)sbi_scratch_thishart_ptr() + (offset))/** HART id to scratch table */ extern struct sbi_scratch *hartid_to_scratch_table[];lib/sbi/sbi_fifo.c +3
-3@@ -66,7 +66,7 @@ static inline void __sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data)if (head >= fifo->num_entries) head = head - fifo->num_entries; -sbi_memcpy (fifo->queue + head * fifo->entry_size, data, fifo->entry_size); +sbi_memcpy ((char *)fifo->queue + head * fifo->entry_size, data, fifo->entry_size); fifo->avail++; }@@ -142,7 +142,7 @@ int sbi_fifo_inplace_update(struct sbi_fifo *fifo, void *in, index = fifo->tail + i;if (index >= fifo->num_entries) index -= fifo->num_entries; - entry = (void *)fifo->queue + (u32 )index * fifo->entry_size; + entry = (char *)fifo->queue + (u32 )index * fifo->entry_size; ret =fptr (in, entry);if (ret ==SBI_FIFO_SKIP || ret ==SBI_FIFO_UPDATED ) {@@ -184,7 +184,7 @@ int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data)return SBI_ENOENT ; } -sbi_memcpy (data, fifo->queue + (u32 )fifo->tail * fifo->entry_size, +sbi_memcpy (data, (char *)fifo->queue + (u32 )fifo->tail * fifo->entry_size, fifo->entry_size); fifo->avail--;lib/sbi/sbi_string.c +2
-2@@ -149,8 +149,8 @@ void *sbi_memmove(void *dest, const void *src, size_t count) count--; } }else { - temp1 = dest + count -1 ; - temp2 = src + count -1 ; + temp1 = (char *)dest + count -1 ; + temp2 = (char *)src + count -1 ;while (count >0 ) { *temp1-- = *temp2--;lib/utils/irqchip/plic.c +4
-4@@ -23,7 +23,7 @@static void plic_set_priority (struct plic_data *plic,u32 source,u32 val) { -volatile void *plic_priority = (void *)plic->addr + +volatile void *plic_priority = (char *)plic->addr +PLIC_PRIORITY_BASE +4 * source;writel (val, plic_priority); }@@ -35,19 +35,19 @@ void plic_set_thresh(struct plic_data *plic, u32 cntxid, u32 val)if (!plic)return ; - plic_thresh = (void *)plic->addr + + plic_thresh = (char *)plic->addr +PLIC_CONTEXT_BASE +PLIC_CONTEXT_STRIDE * cntxid;writel (val, plic_thresh); }void plic_set_ie (struct plic_data *plic,u32 cntxid,u32 word_index,u32 val) { -volatile void *plic_ie; +volatile char *plic_ie;if (!plic)return ; - plic_ie = (void *)plic->addr + + plic_ie = (char *)plic->addr +PLIC_ENABLE_BASE +PLIC_ENABLE_STRIDE * cntxid;writel (val, plic_ie + word_index *4 ); }lib/utils/timer/aclint_mtimer.c +1
-1@@ -47,7 +47,7 @@ static u64 mtimer_time_rd32(volatile u64 *addr)static void mtimer_time_wr32 (bool timecmp,u64 value,volatile u64 *addr) {writel_relaxed ((timecmp) ?-1U :0U , (void *)(addr)); -writel_relaxed ((u32)(value >>32 ), (void *)(addr) +0x04 ); +writel_relaxed ((u32)(value >>32 ), (char *)(addr) +0x04 );writel_relaxed ((u32 )value, (void *)(addr)); }
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
Thanks for comments!
Quoted 9 lines · Damien Le Moal
[Re-sending this since my emails end up being empty...]
Quoted 4 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)
I will add these to patchset v3 !
Quoted 3 lines
I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
You are right, that is is more natural to do arithmetic on integers than pointers.
However, I don't like to change pointer to unsigned long and back for several reasons; one being that afaik no stardard states that sizeof(unsigned long) == sizeof(void *). And even while this is a typical case, there is quarantee that this wouldn't have other side effects (such as padding bits ...).
There is intptr_t and uintptr_t in C99 as optional for this purpose, but those don't exist in all C/C++ standards.
So; I'd rather just do pointer arithmetic when the intention is to do pointer arithmetic :)
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
Quoted 20 lines · Jukka Laitinen
Thanks for comments!
Quoted 9 lines · Damien Le Moal
[Re-sending this since my emails end up being empty...]
Quoted 4 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)I will add these to patchset v3 !
Quoted 3 lines
I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
Quoted 8 lines
You are right, that is is more natural to do arithmetic on integers than pointers.
However, I don't like to change pointer to unsigned long and back for several reasons; one being that afaik no stardard states that sizeof(unsigned long) ==
sizeof(void *). And even while this is a typical case, there is quarantee that this wouldn't have other side effects (such as padding bits ...).
Well, unlike Linux kernel, the nice thing with OpenSBI is that we only need this unsigned long cast to work on risc-v 32 and 64. And as far as I know, it does, and that is mandated by the RISC-V ISA specs.
Quoted 8 lines
There is
intptr_tanduintptr_tin C99 as optional for this purpose, but those don't exist in all C/C++ standards.So; I'd rather just do pointer arithmetic when the intention is to do pointer arithmetic :)
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
Quoted 31 lines · Damien Le Moal
Quoted 20 lines · Jukka Laitinen
Thanks for comments!
Quoted 9 lines · Damien Le Moal
[Re-sending this since my emails end up being empty...]
Quoted 4 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)I will add these to patchset v3 !
Quoted 3 lines
I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
Quoted 8 lines
You are right, that is is more natural to do arithmetic on integers than pointers.
However, I don't like to change pointer to unsigned long and back for several reasons; one being that afaik no stardard states that sizeof(unsigned long) ==
sizeof(void *). And even while this is a typical case, there is quarantee that this wouldn't have other side effects (such as padding bits ...).
Quoted 3 lines
Well, unlike Linux kernel, the nice thing with OpenSBI is that we only need this unsigned long cast to work on risc-v 32 and 64. And as far as I know, it does, and that is mandated by the RISC-V ISA specs.
Except for our work unsigned long *does not work*. We extend RISC-V with new instructions and add a new ABI where pointers are not unsigned long and any attempt to treat them like that will result in crashing when you dereference the result. So please don?t or I will just have to later submit a patch to fix it to use an actual pointer rather than rely on implementation-defined behaviour. Keep the Linux kernel abominations like unsigned long everywhere out of OpenSBI.
Jess
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
Quoted 36 lines · Jessica Clarke
Quoted 31 lines · Damien Le Moal
Quoted 20 lines · Jukka Laitinen
Thanks for comments!
Quoted 9 lines · Damien Le Moal
[Re-sending this since my emails end up being empty...]
Quoted 4 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)I will add these to patchset v3 !
Quoted 3 lines
I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
Quoted 8 lines
You are right, that is is more natural to do arithmetic on integers than pointers.
However, I don't like to change pointer to unsigned long and back for several reasons; one being that afaik no stardard states that sizeof(unsigned long) ==
sizeof(void *). And even while this is a typical case, there is quarantee that this wouldn't have other side effects (such as padding bits ...).Quoted 3 lines
Well, unlike Linux kernel, the nice thing with OpenSBI is that we only need this unsigned long cast to work on risc-v 32 and 64. And as far as I know, it does, and that is mandated by the RISC-V ISA specs.
Quoted 7 lines
Except for our work unsigned long *does not work*. We extend RISC-V with new instructions and add a new ABI where pointers are not unsigned long and any attempt to treat them like that will result in crashing when you dereference the result. So please don?t or I will just have to later submit a patch to fix it to use an actual pointer rather than rely on implementation-defined behaviour. Keep the Linux kernel abominations like unsigned long everywhere out of OpenSBI.
I am not nacking anything here. I am OK with the char * cast even though I find it a bit ugly... u8 * ? Anyway, I am not opposed to the patch as is.
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 1/1 lib: fix pointer of type 'void *' used in arithmetic
In reply to Damien Le MoalQuoted 7 lines · Damien Le Moal
[Re-sending this since my emails end up being empty...]
Quoted 3 lines · Jukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings:
"error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]"
Quoted 7 lines
Quoted 1 line
Just remove these by calculating on "char *" where 1-byte data size is assumed
s/Just Remove/Avoid
(and nit: add a period at the end of the sentence)I would also suggest a cast to unsigned long instead of char *. unsigned long is more common/natural for addresses arithmetic.
No it is not. unsigned long is not a pointer, we are not the Linux kernel, do not introduce that nonsense to OpenSBI. Use char * or uintptr_t.
Jess