diff mbox series

[09/14] tools/console: Use const whenever we point to literal strings

Message ID 20210405155713.29754-10-julien@xen.org (mailing list archive)
State New, archived
Headers show
Series Use const whether we point to literal strings (take 1) | expand

Commit Message

Julien Grall April 5, 2021, 3:57 p.m. UTC
From: Julien Grall <jgrall@amazon.com>

literal strings are not meant to be modified. So we should use const
char * rather than char * when we want to store a pointer to them.

Signed-off-by: Julien Grall <jgrall@amazon.com>
---
 tools/console/client/main.c |  4 ++--
 tools/console/daemon/io.c   | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

Comments

Anthony PERARD May 11, 2021, 3:18 p.m. UTC | #1
On Mon, Apr 05, 2021 at 04:57:08PM +0100, Julien Grall wrote:
> From: Julien Grall <jgrall@amazon.com>
> 
> literal strings are not meant to be modified. So we should use const
> char * rather than char * when we want to store a pointer to them.
> 
> Signed-off-by: Julien Grall <jgrall@amazon.com>
> ---
> diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
> index 4af27ffc5d02..6a8a94e31b65 100644
> --- a/tools/console/daemon/io.c
> +++ b/tools/console/daemon/io.c
> @@ -109,9 +109,9 @@ struct console {
>  };
>  
>  struct console_type {
> -	char *xsname;
> -	char *ttyname;
> -	char *log_suffix;
> +	const char *xsname;

I think that const of `xsname` is lost in console_init() in the same
file.
We have:

    static int console_init(.. )
    {
        struct console_type **con_type = (struct console_type **)data;
        char *xsname, *xspath;
        xsname = (char *)(*con_type)->xsname;
    }

So constify "xsname" in console_init() should be part of the patch, I
think.

Thanks,
Julien Grall May 18, 2021, 1:48 p.m. UTC | #2
Hi Anthony,

On 11/05/2021 16:18, Anthony PERARD wrote:
> On Mon, Apr 05, 2021 at 04:57:08PM +0100, Julien Grall wrote:
>> From: Julien Grall <jgrall@amazon.com>
>>
>> literal strings are not meant to be modified. So we should use const
>> char * rather than char * when we want to store a pointer to them.
>>
>> Signed-off-by: Julien Grall <jgrall@amazon.com>
>> ---
>> diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
>> index 4af27ffc5d02..6a8a94e31b65 100644
>> --- a/tools/console/daemon/io.c
>> +++ b/tools/console/daemon/io.c
>> @@ -109,9 +109,9 @@ struct console {
>>   };
>>   
>>   struct console_type {
>> -	char *xsname;
>> -	char *ttyname;
>> -	char *log_suffix;
>> +	const char *xsname;
> 
> I think that const of `xsname` is lost in console_init() in the same
> file.
> We have:
> 
>      static int console_init(.. )
>      {
>          struct console_type **con_type = (struct console_type **)data;
>          char *xsname, *xspath;
>          xsname = (char *)(*con_type)->xsname;
>      }
> 
> So constify "xsname" in console_init() should be part of the patch, I
> think.

Good point. I am not entirely sure why the cast has been added because 
the field has always been a char *.

Anyway, I will remove it.

Cheers,
diff mbox series

Patch

diff --git a/tools/console/client/main.c b/tools/console/client/main.c
index 088be28dff02..80157be42144 100644
--- a/tools/console/client/main.c
+++ b/tools/console/client/main.c
@@ -325,7 +325,7 @@  int main(int argc, char **argv)
 {
 	struct termios attr;
 	int domid;
-	char *sopt = "hn:";
+	const char *sopt = "hn:";
 	int ch;
 	unsigned int num = 0;
 	int opt_ind=0;
@@ -345,7 +345,7 @@  int main(int argc, char **argv)
 	char *end;
 	console_type type = CONSOLE_INVAL;
 	bool interactive = 0;
-	char *console_names = "serial, pv, vuart";
+	const char *console_names = "serial, pv, vuart";
 
 	while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
 		switch(ch) {
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index 4af27ffc5d02..6a8a94e31b65 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -87,14 +87,14 @@  struct buffer {
 };
 
 struct console {
-	char *ttyname;
+	const char *ttyname;
 	int master_fd;
 	int master_pollfd_idx;
 	int slave_fd;
 	int log_fd;
 	struct buffer buffer;
 	char *xspath;
-	char *log_suffix;
+	const char *log_suffix;
 	int ring_ref;
 	xenevtchn_handle *xce_handle;
 	int xce_pollfd_idx;
@@ -109,9 +109,9 @@  struct console {
 };
 
 struct console_type {
-	char *xsname;
-	char *ttyname;
-	char *log_suffix;
+	const char *xsname;
+	const char *ttyname;
+	const char *log_suffix;
 	bool optional;
 	bool use_gnttab;
 };