Skip to content

Serial: properly protect ringbuffer access #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cores/arduino/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ void UART::on_rx() {
return;
}
#endif
while(_serial->obj->readable()) {
while(_serial->obj->readable() && rx_buffer.availableForStore()) {
char c;
core_util_critical_section_enter();
_serial->obj->read(&c, 1);
rx_buffer.store_char(c);
core_util_critical_section_exit();
}
}

Expand All @@ -160,7 +162,10 @@ int UART::available() {
return _SerialUSB.available();
}
#endif
return rx_buffer.available();
core_util_critical_section_enter();
int c = rx_buffer.available();
core_util_critical_section_exit();
return c;
}

int UART::peek() {
Expand All @@ -169,7 +174,10 @@ int UART::peek() {
return _SerialUSB.peek();
}
#endif
return rx_buffer.peek();
core_util_critical_section_enter();
int c = rx_buffer.peek();
core_util_critical_section_exit();
return c;
}

int UART::read() {
Expand All @@ -178,7 +186,10 @@ int UART::read() {
return _SerialUSB.read();
}
#endif
return rx_buffer.read_char();
core_util_critical_section_enter();
int c = rx_buffer.read_char();
core_util_critical_section_exit();
return c;
}

void UART::flush() {
Expand Down