public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers
@ 2022-02-17 12:55 Mira Limbeck
  2022-02-17 13:24 ` Fabian Ebner
  0 siblings, 1 reply; 5+ messages in thread
From: Mira Limbeck @ 2022-02-17 12:55 UTC (permalink / raw)
  To: pve-devel

'qemu-img info' with output format 'json' returns the size and used values as
integers, but the regex match converts them to strings.
As we know they only contain digits, we can simply cast them back to integers
after the regex.

The API requires them to be integers.

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
---
 PVE/Storage/Plugin.pm | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index 12f1b4b..bcc0cc0 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -892,7 +892,13 @@ sub file_size_info {
     my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
 
     ($size) = ($size =~ /^(\d+)$/) or die "size '$size' not an integer\n"; # untaint
+    # the regex above changes the type of $size to 'string'
+    # we know it is an integer based on the regex above, so simply cast it back
+    # this is required by the API
+    $size = int($size);
     ($used) = ($used =~ /^(\d+)$/) or die "used '$used' not an integer\n"; # untaint
+    # same as $size above
+    $used = int($used);
     ($format) = ($format =~ /^(\S+)$/) or die "format '$format' includes whitespace\n"; # untaint
     if (defined($parent)) {
 	($parent) = ($parent =~ /^(\S+)$/) or die "parent '$parent' includes whitespace\n"; # untaint
-- 
2.30.2





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers
  2022-02-17 12:55 [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers Mira Limbeck
@ 2022-02-17 13:24 ` Fabian Ebner
  2022-02-17 13:33   ` Mira Limbeck
  0 siblings, 1 reply; 5+ messages in thread
From: Fabian Ebner @ 2022-02-17 13:24 UTC (permalink / raw)
  To: pve-devel, m.limbeck

Am 17.02.22 um 13:55 schrieb Mira Limbeck:
> 'qemu-img info' with output format 'json' returns the size and used values as
> integers, but the regex match converts them to strings.
> As we know they only contain digits, we can simply cast them back to integers
> after the regex.
> 
> The API requires them to be integers.
> 

Any reason for not doing it in the API call itself? That would cover all
plugins and future changes.

> Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
> ---
>  PVE/Storage/Plugin.pm | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
> index 12f1b4b..bcc0cc0 100644
> --- a/PVE/Storage/Plugin.pm
> +++ b/PVE/Storage/Plugin.pm
> @@ -892,7 +892,13 @@ sub file_size_info {
>      my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
>  
>      ($size) = ($size =~ /^(\d+)$/) or die "size '$size' not an integer\n"; # untaint
> +    # the regex above changes the type of $size to 'string'
> +    # we know it is an integer based on the regex above, so simply cast it back
> +    # this is required by the API
> +    $size = int($size);
>      ($used) = ($used =~ /^(\d+)$/) or die "used '$used' not an integer\n"; # untaint
> +    # same as $size above
> +    $used = int($used);
>      ($format) = ($format =~ /^(\S+)$/) or die "format '$format' includes whitespace\n"; # untaint
>      if (defined($parent)) {
>  	($parent) = ($parent =~ /^(\S+)$/) or die "parent '$parent' includes whitespace\n"; # untaint




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers
  2022-02-17 13:24 ` Fabian Ebner
@ 2022-02-17 13:33   ` Mira Limbeck
  2022-02-17 14:10     ` Fabian Ebner
  0 siblings, 1 reply; 5+ messages in thread
From: Mira Limbeck @ 2022-02-17 13:33 UTC (permalink / raw)
  To: Fabian Ebner, pve-devel

On 2/17/22 14:24, Fabian Ebner wrote:
> Am 17.02.22 um 13:55 schrieb Mira Limbeck:
>> 'qemu-img info' with output format 'json' returns the size and used values as
>> integers, but the regex match converts them to strings.
>> As we know they only contain digits, we can simply cast them back to integers
>> after the regex.
>>
>> The API requires them to be integers.
>>
> Any reason for not doing it in the API call itself? That would cover all
> plugins and future changes.

The main reason is that we call volume_size_info (which forwards to 
file_size_info in most cases) and file_size_info in other parts of our 
code as well. Wouldn't it be more consistent for `size` and `used` to be 
integers in every context, rather than just in that specific API call?

We could add an additional cast in the API call as well to make sure 
other and future plugins don't run into the same issues.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers
  2022-02-17 13:33   ` Mira Limbeck
@ 2022-02-17 14:10     ` Fabian Ebner
  2022-02-17 14:19       ` Mira Limbeck
  0 siblings, 1 reply; 5+ messages in thread
From: Fabian Ebner @ 2022-02-17 14:10 UTC (permalink / raw)
  To: Mira Limbeck, pve-devel

Am 17.02.22 um 14:33 schrieb Mira Limbeck:
> On 2/17/22 14:24, Fabian Ebner wrote:
>> Am 17.02.22 um 13:55 schrieb Mira Limbeck:
>>> 'qemu-img info' with output format 'json' returns the size and used
>>> values as
>>> integers, but the regex match converts them to strings.
>>> As we know they only contain digits, we can simply cast them back to
>>> integers
>>> after the regex.
>>>
>>> The API requires them to be integers.
>>>
>> Any reason for not doing it in the API call itself? That would cover all
>> plugins and future changes.
> 
> The main reason is that we call volume_size_info (which forwards to
> file_size_info in most cases) and file_size_info in other parts of our
> code as well. Wouldn't it be more consistent for `size` and `used` to be
> integers in every context, rather than just in that specific API call?

It doesn't hurt to do it, but it's not persistent, because Perl has no
real types. If it's used as a string some time later (e.g. printed),
it'll be a "string" again.

When converting to JSON, the result depends on how the variable was last
used, so IMHO it should happen as close to that point as possible.

> 
> We could add an additional cast in the API call as well to make sure
> other and future plugins don't run into the same issues.
>




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers
  2022-02-17 14:10     ` Fabian Ebner
@ 2022-02-17 14:19       ` Mira Limbeck
  0 siblings, 0 replies; 5+ messages in thread
From: Mira Limbeck @ 2022-02-17 14:19 UTC (permalink / raw)
  To: Fabian Ebner, pve-devel

On 2/17/22 15:10, Fabian Ebner wrote:
> Am 17.02.22 um 14:33 schrieb Mira Limbeck:
>> On 2/17/22 14:24, Fabian Ebner wrote:
>>> Am 17.02.22 um 13:55 schrieb Mira Limbeck:
>>>> 'qemu-img info' with output format 'json' returns the size and used
>>>> values as
>>>> integers, but the regex match converts them to strings.
>>>> As we know they only contain digits, we can simply cast them back to
>>>> integers
>>>> after the regex.
>>>>
>>>> The API requires them to be integers.
>>>>
>>> Any reason for not doing it in the API call itself? That would cover all
>>> plugins and future changes.
>> The main reason is that we call volume_size_info (which forwards to
>> file_size_info in most cases) and file_size_info in other parts of our
>> code as well. Wouldn't it be more consistent for `size` and `used` to be
>> integers in every context, rather than just in that specific API call?
> It doesn't hurt to do it, but it's not persistent, because Perl has no
> real types. If it's used as a string some time later (e.g. printed),
> it'll be a "string" again.
>
> When converting to JSON, the result depends on how the variable was last
> used, so IMHO it should happen as close to that point as possible.
>
Yes, that's what lead to this situation in the first place.

I'll send a v2 then with both this change and the cast in the API call.





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-02-17 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-17 12:55 [pve-devel] [PATCH storage] fix #3894: file 'size' and 'used' are not integers Mira Limbeck
2022-02-17 13:24 ` Fabian Ebner
2022-02-17 13:33   ` Mira Limbeck
2022-02-17 14:10     ` Fabian Ebner
2022-02-17 14:19       ` Mira Limbeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal