diff mbox

[v2,5/6] block: mirror - change string allocation to 2-bytes

Message ID 943d55a006aab912af62bbb89833a48baba2d69c.1421768887.git.jcody@redhat.com
State New
Headers show

Commit Message

Jeff Cody Jan. 20, 2015, 5:31 p.m. UTC
The backing_filename string in mirror_run() is only used to check
for a NULL string, so we don't need to allocate 1024 bytes (or, later,
PATH_MAX bytes), when we only need to copy the first 2 characters.

We technically only need 1 byte, as we are just checking for NULL, but
since backing_filename[] is populated by bdrv_get_backing_filename(), a
string size of 1 will always only return '\0';

Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 block/mirror.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

John Snow Jan. 20, 2015, 6:37 p.m. UTC | #1
On 01/20/2015 12:31 PM, Jeff Cody wrote:
> The backing_filename string in mirror_run() is only used to check
> for a NULL string, so we don't need to allocate 1024 bytes (or, later,
> PATH_MAX bytes), when we only need to copy the first 2 characters.
>
> We technically only need 1 byte, as we are just checking for NULL, but
> since backing_filename[] is populated by bdrv_get_backing_filename(), a
> string size of 1 will always only return '\0';
>
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>   block/mirror.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/block/mirror.c b/block/mirror.c
> index 9019d1b..4056164 100644
> --- a/block/mirror.c
> +++ b/block/mirror.c
> @@ -378,7 +378,8 @@ static void coroutine_fn mirror_run(void *opaque)
>       int64_t sector_num, end, sectors_per_chunk, length;
>       uint64_t last_pause_ns;
>       BlockDriverInfo bdi;
> -    char backing_filename[1024];
> +    char backing_filename[2]; /* we only need 2 characters because we are only
> +                                 checking for a NULL string */
>       int ret = 0;
>       int n;
>
>

Reviewed-by: John Snow <jsnow@redhat.com>
Stefan Hajnoczi Jan. 22, 2015, 11:41 a.m. UTC | #2
On Tue, Jan 20, 2015 at 12:31:32PM -0500, Jeff Cody wrote:
> The backing_filename string in mirror_run() is only used to check
> for a NULL string, so we don't need to allocate 1024 bytes (or, later,
> PATH_MAX bytes), when we only need to copy the first 2 characters.
> 
> We technically only need 1 byte, as we are just checking for NULL, but
> since backing_filename[] is populated by bdrv_get_backing_filename(), a
> string size of 1 will always only return '\0';
> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  block/mirror.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
diff mbox

Patch

diff --git a/block/mirror.c b/block/mirror.c
index 9019d1b..4056164 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -378,7 +378,8 @@  static void coroutine_fn mirror_run(void *opaque)
     int64_t sector_num, end, sectors_per_chunk, length;
     uint64_t last_pause_ns;
     BlockDriverInfo bdi;
-    char backing_filename[1024];
+    char backing_filename[2]; /* we only need 2 characters because we are only
+                                 checking for a NULL string */
     int ret = 0;
     int n;