Discussion:
variable ‘something’ set but not used [-Wunused-but-set-variable]
Mahmood Naderan
2011-07-12 11:11:48 UTC
Permalink
dear all,
GCC-4.6 says this warning
   

syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]


However in the code, I see

        int length, fullpath_length;    // warning at this line
        int host_fd;
        struct fd_t *fd;

        /* Read parameters */
        pfilename = isa_regs->ebx;
        flags = isa_regs->ecx;
        mode = isa_regs->edx;
        length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
        if (length >= MAX_PATH_SIZE)
            fatal("syscall open: maximum path length exceeded");
        ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
        fullpath_length = strlen(fullpath);     // variable is used here

 
As you can see fullpath_length is defined as 'int' and is used at the end of the code.
Any way to fix that?


// Naderan *Mahmood;
Axel Freyn
2011-07-12 11:19:43 UTC
Permalink
Hi Mahmood,
Post by Mahmood Naderan
dear all,
GCC-4.6 says this warning
   
syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]
However in the code, I see
        int length, fullpath_length;    // warning at this line
        int host_fd;
        struct fd_t *fd;
        /* Read parameters */
        pfilename = isa_regs->ebx;
        flags = isa_regs->ecx;
        mode = isa_regs->edx;
        length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
        if (length >= MAX_PATH_SIZE)
            fatal("syscall open: maximum path length exceeded");
        ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
        fullpath_length = strlen(fullpath);     // variable is used here
 
As you can see fullpath_length is defined as 'int' and is used at the end of the code.
Any way to fix that?
Gcc is right, the warning is appropriate.
gcc tells you that fullpath_length is not necessary, because while you
ASSIGN a value to it ("variable ... set"), you NEVER READ that value
("but not used").
And in your example code: Imagine we would simply remove
fullpath_length, and replace the last line by "strlen(fullpath);", then
your code would behave exactly as before.
So why do you have this variable fullpath_length?

if you don't want that kind of warning, compile with the flag
-Wno-unused-but-set-variable

Axel
Mahmood Naderan
2011-07-12 18:01:58 UTC
Permalink
dear all,
GCC-4.6 says this warning
   

syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]


However in the code, I see

        int length, fullpath_length;    // warning at this line
        int host_fd;
        struct fd_t *fd;

        /* Read parameters */
        pfilename = isa_regs->ebx;
        flags = isa_regs->ecx;
        mode = isa_regs->edx;
        length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
        if (length >= MAX_PATH_SIZE)
            fatal("syscall open: maximum path length exceeded");
        ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
        fullpath_length = strlen(fullpath);     // variable is used here

 
As you can see fullpath_length is defined as 'int' and is used at the end of the code.
Any way to fix that?


// Naderan *Mahmood;
Andrew Haley
2011-07-12 18:08:39 UTC
Permalink
Post by Mahmood Naderan
dear all,
GCC-4.6 says this warning
syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]
However in the code, I see
int length, fullpath_length; // warning at this line
int host_fd;
struct fd_t *fd;
/* Read parameters */
pfilename = isa_regs->ebx;
flags = isa_regs->ecx;
mode = isa_regs->edx;
length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
if (length >= MAX_PATH_SIZE)
fatal("syscall open: maximum path length exceeded");
ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
fullpath_length = strlen(fullpath); // variable is used here
As you can see fullpath_length is defined as 'int' and is used at the end of the code.
Any way to fix that?
The message is correct. fullpath_length is set, but not used.

Andrew.
Mahmood Naderan
2011-07-12 18:12:25 UTC
Permalink
The message is correct.  fullpath_length is set, but not used.
 
Hi
What is this then

     fullpath_length = strlen(fullpath);    // variable is used here

isn't a usage of fullpath_length?


// Naderan *Mahmood;


----- Original Message -----
From: Andrew Haley <***@redhat.com>
To: gcc-***@gcc.gnu.org
Cc:
Sent: Tuesday, July 12, 2011 10:38 PM
Subject: Re: variable ‘something’ set but not used [-Wunused-but-set-variable]
dear all,
GCC-4.6 says this warning
   
syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]
However in the code, I see
        int length, fullpath_length;    // warning at this line
        int host_fd;
        struct fd_t *fd;
        /* Read parameters */
        pfilename = isa_regs->ebx;
        flags = isa_regs->ecx;
        mode = isa_regs->edx;
        length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
        if (length >= MAX_PATH_SIZE)
            fatal("syscall open: maximum path length exceeded");
        ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
        fullpath_length = strlen(fullpath);    // variable is used here
 
As you can see fullpath_length is defined as 'int' and is used at the end of the code.
Any way to fix that?
The message is correct.  fullpath_length is set, but not used.

Andrew.
Andrew Haley
2011-07-12 18:13:16 UTC
Permalink
Post by Mahmood Naderan
Post by Andrew Haley
The message is correct. fullpath_length is set, but not used.
Hi
What is this then
fullpath_length = strlen(fullpath); // variable is used here
isn't a usage of fullpath_length?
No, it's not. It's a set of fullpath_length.

Andrew.
Mahmood Naderan
2011-07-12 18:21:11 UTC
Permalink
No, it's not.  It's a set of fullpath_length.
Sorry something is not clear for me.
what does this mean

int i;
i = 0;

I defined a variable and used it.

If fullpath_length is not really used, I should not receive any error by commenting that. Like this:
  

int length/*, fullpath_length*/;

But I get this error

syscall.c:1023:3: error: ‘fullpath_length’ undeclared (first use in this function)


the line it points is
fullpath_length = strlen(fullpath);


// Naderan *Mahmood;


----- Original Message -----
From: Andrew Haley <***@redhat.com>
To: Mahmood Naderan <***@yahoo.com>
Cc: gcc <gcc-***@gcc.gnu.org>
Sent: Tuesday, July 12, 2011 10:43 PM
Subject: Re: variable ‘something’ set but not used [-Wunused-but-set-variable]
The message is correct.  fullpath_length is set, but not used.
 
Hi
What is this then
      fullpath_length = strlen(fullpath);    // variable is used here
isn't a usage of fullpath_length?
No, it's not.  It's a set of fullpath_length.

Andrew.
Mahmood Naderan
2011-07-12 18:36:34 UTC
Permalink
Hi all,
I don't know why I don't receive emails from gcc-help correctly.
In the archive I saw Axel Freyn replied this:


Gcc is right, the warning is appropriate.
gcc tells you that fullpath_length is not necessary, because while you
ASSIGN a value to it ("variable ... set"), you NEVER READ that value
("but not used").
And in your example code: Imagine we would simply remove
fullpath_length, and replace the last line by "strlen(fullpath);", then
your code would behave exactly as before.
So why do you have this variable fullpath_length? if you don't want that kind of warning, compile with the flag
-Wno-unused-but-set-variable

Thanks Axel, I understand. I commented fullpath_length = strlen(fullpath);
and there is no problem with that

Thanks





// Naderan *Mahmood;


----- Original Message -----
From: Andrew Haley <***@redhat.com>
To: Mahmood Naderan <***@yahoo.com>
Cc: gcc <gcc-***@gcc.gnu.org>
Sent: Tuesday, July 12, 2011 10:43 PM
Subject: Re: variable ‘something’ set but not used [-Wunused-but-set-variable]
Post by Mahmood Naderan
The message is correct.  fullpath_length is set, but not used.
 
Hi
What is this then
      fullpath_length = strlen(fullpath);    // variable is used here
isn't a usage of fullpath_length?
No, it's not.  It's a set of fullpath_length.

Andrew.

Loading...