arduino / arduino/ArduinoCore-avr

write() doesn't return actually sent bytes in case of buffer overflow

Open
#597 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

The buffer used for transmission is fixed at 32 bytes. Trying to use write() to transmit more than this in one transmission results in only 32 bytes being sent out, but the `write()` function incorrectly returning the amount of bytes originally requested.

Example of writing a page to a 24C EEPROM chip:
```C
void writePage(int addr, char* buf, int len) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(addr); // set write offset address
Wire.endTransmission();
for(int i = 0; i < len;) {
Wire.beginTransmission(I2C_ADDRESS);
i += Wire.write(buf + i, len - i); // i will immediately be set to len and loop will terminate prematurely
Wire.endTransmission();
}
}

writePage(0, data, 256);
```
In the above example I am trying to overcome the 32 byte buffer limitation by paging writes to whatever size `write()` actually writes out, however it will always return the full length, making the code believe everything was sent out. Instead I would have to limit my transmission to `BUFFER_LENGTH` manually. `write()` should check if the requested amount of data is exceeding the buffer and then return the amount that is actually still able to be sent out.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the Wire write() implementation and the 32-byte buffer limit represented by BUFFER_LENGTH. Reproduce the 256-byte EEPROM example, then verify that write() reports the number of bytes actually accepted when the request exceeds the remaining buffer space.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.