Skip to content

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

Open
@SpaghettiBorgar

Description

@SpaghettiBorgar

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions