[pacman-dev] Bug in libalpm: sizeof(off_t)

Allan McRae allan at archlinux.org
Wed Dec 11 12:09:05 EST 2013


On 12/12/13 03:02, Allan McRae wrote:
> On 12/12/13 02:44, Dave Reisner wrote:
>> On Thu, Dec 12, 2013 at 02:14:19AM +1000, Allan McRae wrote:
>>> On 12/12/13 00:24, Jeremy Heiner wrote:
>>>> On Tue, Dec 10, 2013 at 11:58 PM, Allan McRae <allan at archlinux.org> wrote:
>>>>> I am still looking for something cleaner than
>>>>> the proposed #define/#undef approach which feels a bit hacky
>>>>
>>>> Here is a third (well, 4th, since "do nothing" is always on the table)
>>>> option which lies between "just document" and "macro shenanigans"...
>>>> Following the lead of 'alpm_capabilities', add:
>>>>
>>>
>>> How about #5...
>>>
>>>
>>> diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c
>>> index 878c38b..a41c07d 100644
>>> --- a/lib/libalpm/alpm.c
>>> +++ b/lib/libalpm/alpm.c
>>> @@ -52,6 +52,12 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char
>>> *root, const char *dbpath,
>>>  	const char *lf = "db.lck";
>>>  	size_t lockfilelen;
>>>  	alpm_handle_t *myhandle = _alpm_handle_new();
>>> +
>>> +	/* calculate off_t size at runtime */
>>> +	size_t off_t_size = ((char *)((off_t *)0 + 1) - (char *)(off_t *)0);
>>> +
>>> +	if(off_t_size != sizeof(off_t)) {
>>> +		myerr = ALPM_ERR_OFF_T_SIZE;
>>> +		goto cleanup;
>>> +	}
>>
>> I do not believe this actually works as intended. There's nothing "at
>> runtime" about this, especially when optimizations are involved (even
>> with -O1). Your off_t_size will be calculated at compile time and
>> therefore the comparison that follows will never fail. The instructions
>> generated for the off_t_size is effectively:
>>
>>     mov    $0x8,%eax
>>
>> Or, when LFS isn't enabled:
>>
>>     mov   $0x4,%eax
>>
> 
> This version looks like it should survive optimization:
> 
> 	off_t x;
> 	size_t off_t_size = ((char *)(&(x) + 1) - (char *)&(x));
> 

And nope...  smart compiler.




More information about the pacman-dev mailing list