* [PATCH] fs/nilfs2: prevent int overflow in btree binary search @ 2024-04-02 18:00 Sabyrzhan Tasbolatov 2024-04-02 20:55 ` Ryusuke Konishi 0 siblings, 1 reply; 3+ messages in thread From: Sabyrzhan Tasbolatov @ 2024-04-02 18:00 UTC (permalink / raw) To: konishi.ryusuke, linux-nilfs; +Cc: linux-kernel, snovitoll Should prevent int overflow if low + high > INT_MAX in big btree with nchildren in nilfs_btree_node_lookup() binary search. Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com> --- fs/nilfs2/btree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c index 65659fa03..39ee4fe11 100644 --- a/fs/nilfs2/btree.c +++ b/fs/nilfs2/btree.c @@ -300,7 +300,7 @@ static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node, index = 0; s = 0; while (low <= high) { - index = (low + high) / 2; + index = low + (high - low) / 2; nkey = nilfs_btree_node_get_key(node, index); if (nkey == key) { s = 0; -- 2.34.1 ^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] fs/nilfs2: prevent int overflow in btree binary search 2024-04-02 18:00 [PATCH] fs/nilfs2: prevent int overflow in btree binary search Sabyrzhan Tasbolatov @ 2024-04-02 20:55 ` Ryusuke Konishi 2024-04-03 17:08 ` Sabyrzhan Tasbolatov 0 siblings, 1 reply; 3+ messages in thread From: Ryusuke Konishi @ 2024-04-02 20:55 UTC (permalink / raw) To: Sabyrzhan Tasbolatov; +Cc: linux-nilfs, linux-kernel On Wed, Apr 3, 2024 at 3:00 AM Sabyrzhan Tasbolatov wrote: > > Should prevent int overflow if low + high > INT_MAX in big btree with > nchildren in nilfs_btree_node_lookup() binary search. > > Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com> > --- > fs/nilfs2/btree.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c > index 65659fa03..39ee4fe11 100644 > --- a/fs/nilfs2/btree.c > +++ b/fs/nilfs2/btree.c > @@ -300,7 +300,7 @@ static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node, > index = 0; > s = 0; > while (low <= high) { > - index = (low + high) / 2; > + index = low + (high - low) / 2; > nkey = nilfs_btree_node_get_key(node, index); > if (nkey == key) { > s = 0; > -- > 2.34.1 > Hi Sabyrzhan, Thank you for your interesting patch. In this function, the value of the variable "high" is initialized with "nilfs_btree_node_get_nchildren() - 1", and "low" is initialized with 0. nilfs_btree_node_get_nchildren() returns a value read from a 16-bit wide field, so it will never exceed U16_MAX. These index calculations narrow the range between "low" and "high", so as long as INT_MAX is 32-bit or more, it seems that the calculation of this intermediate value will not overflow. So while it's a good overflow avoidance technique, it doesn't seem to happen in practice. Am I missing something? Regards, Ryusuke Konishi ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] fs/nilfs2: prevent int overflow in btree binary search 2024-04-02 20:55 ` Ryusuke Konishi @ 2024-04-03 17:08 ` Sabyrzhan Tasbolatov 0 siblings, 0 replies; 3+ messages in thread From: Sabyrzhan Tasbolatov @ 2024-04-03 17:08 UTC (permalink / raw) To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel Hi Ryusuke, > nilfs_btree_node_get_nchildren() returns a value read from a 16-bit > wide field, so it will never exceed U16_MAX. You're right, "high" indeed never exceeds INT_MAX as it's limited to 16-bit in 32-bit integer. Sorry for the confusion, It landed via my grepping tool. Sabyrzhan Tasbolatov On Wed, Apr 3, 2024 at 1:55 AM Ryusuke Konishi <konishi.ryusuke@gmail.com> wrote: > > On Wed, Apr 3, 2024 at 3:00 AM Sabyrzhan Tasbolatov wrote: > > > > Should prevent int overflow if low + high > INT_MAX in big btree with > > nchildren in nilfs_btree_node_lookup() binary search. > > > > Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com> > > --- > > fs/nilfs2/btree.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c > > index 65659fa03..39ee4fe11 100644 > > --- a/fs/nilfs2/btree.c > > +++ b/fs/nilfs2/btree.c > > @@ -300,7 +300,7 @@ static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node, > > index = 0; > > s = 0; > > while (low <= high) { > > - index = (low + high) / 2; > > + index = low + (high - low) / 2; > > nkey = nilfs_btree_node_get_key(node, index); > > if (nkey == key) { > > s = 0; > > -- > > 2.34.1 > > > > Hi Sabyrzhan, > > Thank you for your interesting patch. > > In this function, the value of the variable "high" is initialized with > "nilfs_btree_node_get_nchildren() - 1", and "low" is initialized with > 0. > > nilfs_btree_node_get_nchildren() returns a value read from a 16-bit > wide field, so it will never exceed U16_MAX. > > These index calculations narrow the range between "low" and "high", so > as long as INT_MAX is 32-bit or more, it seems that the calculation of > this intermediate value will not overflow. > > So while it's a good overflow avoidance technique, it doesn't seem to > happen in practice. > > Am I missing something? > > Regards, > Ryusuke Konishi ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-04-03 17:08 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-04-02 18:00 [PATCH] fs/nilfs2: prevent int overflow in btree binary search Sabyrzhan Tasbolatov 2024-04-02 20:55 ` Ryusuke Konishi 2024-04-03 17:08 ` Sabyrzhan Tasbolatov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).