0

Can this code cause a buffer overflow when unexpected messages are delivered? Also the expected messages are simple "1" and "-1".

char rcv[64] {};
  int i = 0;
  while (modem.available()) {
    rcv[i++] = (char)modem.read();
  }
  String data_received = rcv;
  if (data_received == "") {
    Serial.println("Null");
  } else {
    correction_var = data_received.toInt();
  }

Since my arduino's RTC suddenly started acting strange and I can't find a proper reason for this, except that before he went nuts apparently a message failed to be sent to the Arduino. This bit of code right here handles messages received so maybe something went outside of what it should have and messed with the RTC's alarm variables?

Update: So none of this was NOT a buffer overflow problem, the Ardunino's RTC apparently has some sort of problem since even after resetting the code, the problem persists, this time from the beginning, and such I decided to create a new thread Here.

4
  • 3
    if modem.available() returns true more than 64 times, yes. Commented Feb 19, 2019 at 13:22
  • 2
    while (modem.available()) -- Try while (i < 64 && modem.available()) Commented Feb 19, 2019 at 13:23
  • Oh, will give it a try, it's really weird since in months this is actually the first time this happened. Thank you Commented Feb 19, 2019 at 13:27
  • 2
    @BryceSoker a buffer overflow never happens until it happens.... Commented Feb 19, 2019 at 13:28

2 Answers 2

4

You are simply reading an unlimited amount of values without checking if your buffer is full. From your code:

char rcv[64] {};
int i = 0;
while (modem.available()) {
    rcv[i++] = (char)modem.read();
}

It's quite obvious your char-array rcv will overflow after receiving 64 chars. Maybe you should stop reading once your receive-buffer is full?

char rcv[64] {};
int i = 0;
while (modem.available() && i < 64) {
    rcv[i++] = (char)modem.read();
}

Or you could overwrite the oldest value after reading new ones.

char rcv[64] {};
int i = 0;
while (modem.available()) {
    rcv[i % 64] = (char)modem.read();
    ++i;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I will mark this one as accepted answer since it would fix the buffer overflow using that first solution of stop reading it, and monitor the thing to see if it doesn't happen again.
Naively overwriting the oldest value after reading new ones is probably not the right solution.
That solely depends on what the program is supposed to do. Theres no problem if all you care about is the mean value of the last N measurements. If every single data point is of importance, overwriting is of course not ideal.
@markus-nm I would switch the comparison to do the i check first. It will be less expensive if i == 64, since you wouldn't be making the call to modem.available().
2

If modem.available() returns true when i reaches 64, then you will have a buffer overflow.

Additionally, String data_received = rcv; may look for a null terminator (I don't know how String is implemented) in rcv, which may not be there and cause String to read past the end of the rcv buffer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.