Skip to content

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

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

Open
SpaghettiBorgar opened this issue May 26, 2025 · 0 comments

Comments

@SpaghettiBorgar
Copy link

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant