util: fix line length calc in _alpm_archive_fgets

74274b5dc3 which added the real_line_size to the buffer struct
didn't properly account for what happens when archive_fgets has to loop
more than once to find the end of a line. In most cases, this isn't a
problem, but could potentially cause a longer line such as PGP signature
to be improperly read.

This patch fixes the oversight and focuses on only calculating the line
length when we hit the end of line marker. The effective length is then
calculated via pointer arithmetic as:

  (start_of_last_read + read_length) - start_of_line

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
This commit is contained in:
Dave Reisner 2012-07-19 10:37:56 -04:00 committed by Dan McGee
parent 35ac4e7ef3
commit ddbd36103d
1 changed files with 4 additions and 2 deletions

View File

@ -1011,15 +1011,16 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
}
if(eol) {
size_t len = b->real_line_size = (size_t)(eol - b->block_offset);
size_t len = (size_t)(eol - b->block_offset);
memcpy(b->line_offset, b->block_offset, len);
b->line_offset[len] = '\0';
b->block_offset = eol + 1;
b->real_line_size = b->line_offset + len - b->line;
/* this is the main return point; from here you can read b->line */
return ARCHIVE_OK;
} else {
/* we've looked through the whole block but no newline, copy it */
size_t len = b->real_line_size = (size_t)(b->block + b->block_size - b->block_offset);
size_t len = (size_t)(b->block + b->block_size - b->block_offset);
memcpy(b->line_offset, b->block_offset, len);
b->line_offset += len;
b->block_offset = b->block + b->block_size;
@ -1027,6 +1028,7 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
* returned on next call */
if(len == 0) {
b->line_offset[0] = '\0';
b->real_line_size = b->line_offset - b->line;
return ARCHIVE_OK;
}
}