Angel Ring

Angel Ring

23M Downloads

[All versions] Integer overflow without checks

BloCamLimb opened this issue ยท 1 comments

commented

You write final int newValue = stored + maxReceive;, if maxReceive is Integer.MAX_VALUE, it will overflow.

You should add
maxReceive = Math.max(0, Math.min(maxReceive, getMaxEnergyStored() - stored));
before
final int newValue = stored + maxReceive; The Link
and remove
final int finalValue = Math.min(Math.max(newValue, 0), getMaxEnergyStored());

Example:

@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
	final int stored = getEnergyStored();
	final int added = Math.max(0, Math.min(maxReceive, getMaxEnergyStored() - stored));
	if (!simulate)
		stack.getOrCreateTag().putInt("energy", stored + added);
	return added;
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
	final int stored = getEnergyStored();
	final int removed = Math.max(0, Math.min(maxExtract, stored));
	if (!simulate)
		stack.getOrCreateTag().putInt("energy", stored - removed);
	return removed;
}

SonarSonic/Flux-Networks#481

I think you suck at programming.

commented

Thank you for the report and crossreporting to this repo! Sorry for this issue. I will fix this in 2.0.2.

I think you suck at programming.

I never hid it, but I try my best to make my mods better, even there are tons of bugs/weird code. Even that code snippet is from PR from another guy, that's my fault, that I haven't checked that for overflowing. Thank you again.