i would make this easier to follow with something like

if (offset > size) {
  length = 0;
} else if (offset + length > size) {
  length = size - offset;
}