[ 
https://issues.apache.org/jira/browse/LUCENE-8533?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16655031#comment-16655031
 ] 

Uwe Schindler commented on LUCENE-8533:
---------------------------------------

OK, I committed this change. IMHO, changing this is a larger task (it's not 
only the -1 you mentioned). So I am closing this issue. If we want to change 
this, we should open a new issue and plan the change.

> DataInput#readVInt() supports negative numbers although not documented
> ----------------------------------------------------------------------
>
>                 Key: LUCENE-8533
>                 URL: https://issues.apache.org/jira/browse/LUCENE-8533
>             Project: Lucene - Core
>          Issue Type: Improvement
>            Reporter: Vladimir Dolzhenko
>            Assignee: Uwe Schindler
>            Priority: Major
>         Attachments: LUCENE-8533_fix_readVInt_javadoc.patch, readVInt.patch
>
>
> {{readVInt()}} has to return positive numbers (and zero), throw some 
> exception in case of negative numbers.
> While for the sequence of bytes {{[-1, -1, -1, -1, 15]}} it returns {{-1}}.
> simplifying 
> [readVInt|https://github.com/apache/lucene-solr/blob/1d85cd783863f75cea133fb9c452302214165a4d/lucene/core/src/java/org/apache/lucene/store/DataInput.java#L113]
>  up to last readByte (exclusive):
> {code:java}
> int i = ((byte)-1) & 0x7F;
> i |= (((byte)-1) & 0x7F) << 7;
> i |= (((byte)-1) & 0x7F) << 14;
> i |= (((byte)-1) & 0x7F) << 21;
> {code}
> Here {{i = 268435455}} or in binary format is 
> {{00001111_11111111_11111111_11111111}}
> Keeping in mind that {{int}} is a signed type we have only 3 more bits before 
> overflow happens or in another words {{(Integer.MAX_VALUE - i) >> 28 == 7}} - 
> that's max value could be stored in 5th byte to avoid overflow.
> Instead of 
> {code:java}
> i |= (b & 0x0F) << 28;
> if ((b & 0xF0) == 0) return i;
> {code}
> has to be
> {code:java}
> i |= (b & 0x07) << 28;
> if ((b & 0xF8) == 0) return i;
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to