Christian Brabandt wrote:

> Hi Dominique!
>
> On Do, 28 Jul 2011, Dominique Pellé wrote:
>
>> Christian Brabandt  wrote:
>>
>> > Hi Bram!
>> >
>> > On Do, 28 Jul 2011, Bram Moolenaar wrote:
>> >
>> >>
>> >> Christian Brabandt wrote:
>> >>
>> >> > Hi Bram,
>> >> >
>> >> > attached patch fixes this issue from the todo list:
>> >> >
>> >> > ,----
>> >> > | Recognize and ignore BOM in error file. (Aleksey Baibarin)
>> >> > `----
>> >>
>> >> The patch was empty...
>> >
>> > I wonder how that happened.
>> > Anyway here is the patch again.
>> >
>> > regards
>> > Christian
>>
>>
>> Hi Christian
>>
>> Looking at the patch, I see...
>>
>> +     /* it should be safe to use the len variable */
>> +     len = STRLEN(IObuff);
>> +     if (check_bomb
>> +         && STRLEN(IObuff) >= 3
>> +         && enc_utf8
>> +         && IObuff[0] == 0xef
>> +         && IObuff[1] == 0xbb
>> +         && IObuff[2] == 0xbf)
>> +     /* remove utf-8 byte order mark from file */
>> +     {
>> +         mch_memmove(IObuff, IObuff+3, len - 3);
>> +         IObuff[len-3] = NUL;
>> +         check_bomb = FALSE;
>> +     }
>>
>>
>> It's calling STRLEN(IOBuff) twice.
>
> Oh yes, I forgot to use len at the second time of STRLEN
>
>> The check for STRLEN(IObuff) >= 3 is not
>> needed anyway since checking for the 3 bytes
>> 0xef, 0xbb, 0xbf will guarantee that STRLEN(IObuff) >= 3.
>
> Thanks for your update. But wouldn't there a null pointer exception, if
> IObuff is less than 3 bytes long? That's why I included the check for
> the length, because if it would be shorter, the expression would
> evaluate to false and the last conditions wouldn't be checked anymore (I
> think)


It is safe without checking (STRLEN(IObuff) >= 3) because
the  C operator '&&' short-circuits. So if length is 0 for example,
then the tests...

 +         && IObuff[0] == 0xef
 +         && IObuff[1] == 0xbb
 +         && IObuff[2] == 0xbf)

... stop as soon as it finds IObuff[0] being end of string (NUL)
since it's different than 0xef, it it never evaluates IObuff[1] or
IObuff[2].  See:

http://en.wikipedia.org/wiki/Short-circuit_evaluation

Regards
-- Dominique

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Raspunde prin e-mail lui