[PATCH] gpu: nova-core: vbios: use size_of instead of magic number

Rhys Lloyd posted 1 patch 2 months, 3 weeks ago
There is a newer version of this series
drivers/gpu/nova-core/vbios.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] gpu: nova-core: vbios: use size_of instead of magic number
Posted by Rhys Lloyd 2 months, 3 weeks ago
12 is identical to the value of `size_of::<BitHeader>()`,
so use the latter instead.

Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
---
 drivers/gpu/nova-core/vbios.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
index 663fc50e8b66..ac35415b4ffb 100644
--- a/drivers/gpu/nova-core/vbios.rs
+++ b/drivers/gpu/nova-core/vbios.rs
@@ -365,7 +365,7 @@ struct BitHeader {
 
 impl BitHeader {
     fn new(data: &[u8]) -> Result<Self> {
-        if data.len() < 12 {
+        if data.len() < core::mem::size_of::<Self>() {
             return Err(EINVAL);
         }
 

base-commit: 215a3f91713383a3c0d2da82d223a608a3c17ac1
-- 
2.50.1
Re: [PATCH] gpu: nova-core: vbios: use size_of instead of magic number
Posted by Alexandre Courbot 2 months, 3 weeks ago
On Mon Jul 14, 2025 at 7:43 PM JST, Rhys Lloyd wrote:
> 12 is identical to the value of `size_of::<BitHeader>()`,
> so use the latter instead.
>
> Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
> ---
>  drivers/gpu/nova-core/vbios.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
> index 663fc50e8b66..ac35415b4ffb 100644
> --- a/drivers/gpu/nova-core/vbios.rs
> +++ b/drivers/gpu/nova-core/vbios.rs
> @@ -365,7 +365,7 @@ struct BitHeader {
>  
>  impl BitHeader {
>      fn new(data: &[u8]) -> Result<Self> {
> -        if data.len() < 12 {
> +        if data.len() < core::mem::size_of::<Self>() {

I agree, but would feel better if we also made `BitHeader` `#[repr(C)]`
to really guarantee this.

(or please educate me if this is unneeded :))