Ignotus-mago / PixelAudio

PixelAudio maps arrays of audio samples onto arrays of pixel values.
Other
0 stars 0 forks source link

Array bounds issues in PixelAudioMapper #13

Open Ignotus-mago opened 4 months ago

Ignotus-mago commented 4 months ago

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:

  1. Just throw the error and teach the coder on the other end to check values before calling.
  2. Reset length if it's too big, but post a warning to System.out about it.
  3. 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;
}