Re: [syzbot] [mtd?] divide error in block2mtd_setup2

From: Jörn Engel

Date: Tue Aug 11 2026 - 14:22:44 EST


On Tue, Aug 11, 2026 at 11:02:53AM +0800, Pei Xiao wrote:
> >
> Thank you for your reply. I am waiting for a reply on whether the cast
> on 'long' needs to be removed. Once that is confirmed, I would be happy
> to do it. Thank you.

It doesn't need to be removed, but it should be removed. Any
unnecessary code should be removed in general and unnecessary casts in
particular should.

One of better parts of the C language is that casts are hardly ever
necessary. Which means that any remaining necessary casts should
immediately draw attention of the reader - something tricky and
potentially dangerous is going on here.

But you stop paying attention when things become too common. Which
means that unnecessary casts are not just noise, they actively do harm.


In this particular case, we can simply test whether the cast makes any
difference:

#include <assert.h>
#include <stdio.h>

int main(void)
{
for (int i=0; i<=1<<16; i++) {
for (int k=1; k<=1<<16; k++) {
long long size = (long long)i << 32;
int erase_size = k;

long long a = size % erase_size;
long long b = (long)size % erase_size;
assert(a==b);
}
}
}

Compile with optimizations and the compiler decides to remove the loop.
Unless you suspect a compiler bug, I guess that settles the question. ;)

Jörn

--
Semper ubi sub ubi ubique.
-- latin pun