I would do something along the following lines:
bool
updateConfigParams( void ) {
char buffer[512] = {};
int i = 0;
while( (c = readFromWireless()) != NULL ) {
if( i == sizeof(buffer) - 1 ) {
warn("readFromWireless exceeded %zu byte limit", sizeof(buffer));
return false;
}
buffer[i++] = c;
}
writeConfigParams( buffer );
return true;
}
Depending on the state of your program, it might be more appropriate simply to call err(3). Important points are:
- When writing to an array, always ensure you're in bounds.
- When collecting input into an array, always be prepared for input that exceeds the array's size. What to do with input you can't accept is application-dependent.
- When internal storage is exceeded by an action the programmer can't prevent at compile time -- such as a wireless device sending "too much" data -- inform the user and the program. Above, the program emits a message, and the function returns an error status.
- Partial input is usually suspect and should not be accepted.
Edit: pursuant to a comment, I added initialization to buffer
. Since writeConfigParams
takes no length parameter, perhaps it accepts a NUL-terminated string.