close(2) should be called after errno is examined as close may change errno variable.
- R10:741d78126b55: rtld: call close(2) after errno is saved
No functional changes intended.
Well, then free() can obliterate errno as well.
I suggest to return to your initial patch, with some modifications.
Note that errno is not set by read(2) unless return value is -1, while code checks both for errors, EOFs, and short reads.
Save the value of errno somewhere if read returned -1, and then use the saved value in dbg().
Please use diff -U99999999 when generating patch, to add context to the review.
We call such variable saved_errno in other places.
I suggest to issue different dbg() statements for case retval == -1 and retval >= 0. In the first case, use saved_errno, in second report something like 'short read' and print expected and returned lengths.