As already noted in other issues and in the comments for PixelAudioMapper, when signalPos + length > img.length we are in trouble. Even more so if signalPos > img.length. There are several ways to handle this:
Just throw the error and teach the coder on the other end to check values before calling.
Reset length if it's too big, but post a warning to System.out about it.
If signalPos is too big, return null.
If this is meant to be performance software, gracefully failing as in 2. is better than wrist-slapping as in 1. I don't particularly want to oblige end users to check for null returns, but they can get a warning and the null array can cause the next error if they continue without checking for null.
All of this checking comes before running loops, so it's very quick to do. It looks like this:
// We can have an error condition if signalPos + length exceeds img.length!
// If signalPos exceeds length, we return null. Let the caller take note and remedy the problem.
if (signalPos + length > img.length) {
length = img.length - signalPos ;
System.out.println("WARNING! signalPos + length exceeded img array length. Length was trimmed to "+ length);
}
if (signalPos >= img.length) {
System.out.println("WARNING! signalPos "+ signalPos +" exceeded img length "+ img.length +". Returning null.");
return null;
}
As already noted in other issues and in the comments for PixelAudioMapper, when
signalPos + length > img.length
we are in trouble. Even more so ifsignalPos > img.length
. There are several ways to handle this:If this is meant to be performance software, gracefully failing as in 2. is better than wrist-slapping as in 1. I don't particularly want to oblige end users to check for null returns, but they can get a warning and the null array can cause the next error if they continue without checking for null.
All of this checking comes before running loops, so it's very quick to do. It looks like this: