[pacman-dev] [PATCH] Check return value of fgets calls

Allan McRae allan at archlinux.org
Sun Jun 27 09:03:23 EDT 2010


On 27/06/10 21:48, Nathan Wayde wrote:
> On 27/06/10 12:45, Allan McRae wrote:
>> Prevents compiler warnings with -D_FORTIFY_SOURCE=2
>>
>> Signed-off-by: Allan McRae<allan at archlinux.org>
>> ---
>> lib/libalpm/be_files.c | 8 ++++++--
>> lib/libalpm/trans.c | 3 +--
>> 2 files changed, 7 insertions(+), 4 deletions(-)
>>
>> diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
>> index c26bef2..e2bbe28 100644
>> --- a/lib/libalpm/be_files.c
>> +++ b/lib/libalpm/be_files.c
>> @@ -657,7 +657,9 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
>> pmdbinfrq_t inforeq)
>> goto error;
>> }
>> while(!feof(fp)) {
>> - fgets(line, 255, fp);
>> + if(fgets(line, 256, fp) == NULL) {
>> + break;
>> + }
>> _alpm_strtrim(line);
>> if(strcmp(line, "%DEPENDS%") == 0) {
>> while(fgets(line, sline, fp)&& strlen(_alpm_strtrim(line))) {
>> @@ -693,7 +695,9 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
>> pmdbinfrq_t inforeq)
>> snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
>> if((fp = fopen(path, "r"))) {
>> while(!feof(fp)) {
>> - fgets(line, 255, fp);
>> + if(fgets(line, 256, fp) == NULL) {
>> + break;
>> + }
>> _alpm_strtrim(line);
>> if(strcmp(line, "%DELTAS%") == 0) {
>> while(fgets(line, sline, fp)&& strlen(_alpm_strtrim(line))) {
>> diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
>> index c042f09..6ebfa8f 100644
>> --- a/lib/libalpm/trans.c
>> +++ b/lib/libalpm/trans.c
>> @@ -324,8 +324,7 @@ static int grep(const char *fn, const char *needle)
>> while(!feof(fp)) {
>> char line[1024];
>> int sline = sizeof(line)-1;
>> - fgets(line, sline, fp);
>> - if(feof(fp)) {
>> + if(fgets(line, sline, fp) == NULL || feof(fp)) {
>> continue;
>> }
>> if(strstr(line, needle)) {
>
> Feel free to ignore this it's not an error or anything. Just wanted to
> let you know that you don't need to check for feof, as fgets will return
> NULL if EOF is reached.
>

Good point.  I remove that check on my working branch.

Thanks,
Allan



More information about the pacman-dev mailing list