This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH c++ 06/12] Fix constness problem in ioscm_make_gdb_stdio_port
- From: Pedro Alves <palves at redhat dot com>
- To: Simon Marchi <simon dot marchi at polymtl dot ca>, gdb-patches at sourceware dot org
- Date: Mon, 26 Oct 2015 16:09:52 +0000
- Subject: Re: [PATCH c++ 06/12] Fix constness problem in ioscm_make_gdb_stdio_port
- Authentication-results: sourceware.org; auth=none
- References: <1445831204-16588-1-git-send-email-simon dot marchi at polymtl dot ca> <1445831204-16588-6-git-send-email-simon dot marchi at polymtl dot ca>
On 10/26/2015 03:46 AM, Simon Marchi wrote:
> ioscm_make_gdb_stdio_port passes const char pointers (literal strings) to
> scm_mode_bits, which takes a non-const char pointer. Ideally, we would
> change scm_mode_bits to take a const char pointer, but it's not part of
> an API we control.
>
> Instead, it's easy enough to build the string to pass to scm_mode_bits in
> a (non-const) char array and pass that.
>
> gdb/ChangeLog:
>
> * guile/scm-ports.c (ioscm_make_gdb_stdio_port): Pass non-const
> char pointer to scm_mode_bits.
> ---
> gdb/guile/scm-ports.c | 17 +++++++++++++----
> 1 file changed, 13 insertions(+), 4 deletions(-)
>
> diff --git a/gdb/guile/scm-ports.c b/gdb/guile/scm-ports.c
> index 925f3b2..49e4fd6 100644
> --- a/gdb/guile/scm-ports.c
> +++ b/gdb/guile/scm-ports.c
> @@ -357,29 +357,38 @@ ioscm_init_stdio_buffers (SCM port, long mode_bits)
> static SCM
> ioscm_make_gdb_stdio_port (int fd)
> {
> - int is_a_tty = isatty (fd);
> const char *name;
> long mode_bits;
> SCM port;
> + char buf[3];
> +
> + memset (buf, 0, sizeof (buf));
>
> switch (fd)
> {
> case 0:
> name = input_port_name;
> - mode_bits = scm_mode_bits (is_a_tty ? "r0" : "r");
> + buf[0] = 'r';
> break;
> case 1:
> name = output_port_name;
> - mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
> + buf[0] = 'w';
> break;
> case 2:
> name = error_port_name;
> - mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
> + buf[0] = 'w';
> break;
> default:
> gdb_assert_not_reached ("bad stdio file descriptor");
> }
>
> + if (isatty (fd))
> + {
> + buf[1] = '0';
> + }
> +
> + mode_bits = scm_mode_bits (buf);
> +
> port = ioscm_open_port (stdio_port_desc, mode_bits);
>
> scm_set_port_filename_x (port, gdbscm_scm_from_c_string (name));
>
Hmmm, I have the below on my branch, which seems clearer/straightforward
to me. I don't think the cast in the scm_mode_bits call is problem.
It's a deficiency of the scm_mode_bits api that it doesn't take a const
pointer, and we actually already have this in ioscm_parse_mode_bits:
/* Kinda awkward to convert the mode from SCM -> string only to have Guile
convert it back to SCM, but that's the API we have to work with. */
mode_bits = scm_mode_bits ((char *) mode);
WDYT?
>From f1038881f2b79d26968f2552b86612324fdf72c3 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Thu, 22 Oct 2015 11:43:57 +0100
Subject: [PATCH] guile: 'const char *' -> 'char *' casts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
/home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c: In function ‘scm_unused_struct* ioscm_make_gdb_stdio_port(int)’:
/home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:369:55: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
mode_bits = scm_mode_bits (is_a_tty ? "r0" : "r");
^
In file included from /usr/include/guile/2.0/libguile/fports.h:28:0,
from /usr/include/guile/2.0/libguile.h:55,
from /home/pedro/gdb/mygit/src/gdb/guile/guile-internal.h:29,
from /home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:28:
/usr/include/guile/2.0/libguile/ports.h:281:14: error: initializing argument 1 of ‘long int scm_mode_bits(char*)’ [-fpermissive]
SCM_API long scm_mode_bits (char *modes);
^
/home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:373:55: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
^
In file included from /usr/include/guile/2.0/libguile/fports.h:28:0,
from /usr/include/guile/2.0/libguile.h:55,
from /home/pedro/gdb/mygit/src/gdb/guile/guile-internal.h:29,
from /home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:28:
/usr/include/guile/2.0/libguile/ports.h:281:14: error: initializing argument 1 of ‘long int scm_mode_bits(char*)’ [-fpermissive]
SCM_API long scm_mode_bits (char *modes);
^
/home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:377:55: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
^
In file included from /usr/include/guile/2.0/libguile/fports.h:28:0,
from /usr/include/guile/2.0/libguile.h:55,
from /home/pedro/gdb/mygit/src/gdb/guile/guile-internal.h:29,
from /home/pedro/gdb/mygit/src/gdb/guile/scm-ports.c:28:
/usr/include/guile/2.0/libguile/ports.h:281:14: error: initializing argument 1 of ‘long int scm_mode_bits(char*)’ [-fpermissive]
SCM_API long scm_mode_bits (char *modes);
---
gdb/guile/scm-ports.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/gdb/guile/scm-ports.c b/gdb/guile/scm-ports.c
index 90bdb39..d8ee70e 100644
--- a/gdb/guile/scm-ports.c
+++ b/gdb/guile/scm-ports.c
@@ -359,6 +359,7 @@ ioscm_make_gdb_stdio_port (int fd)
{
int is_a_tty = isatty (fd);
const char *name;
+ const char *mode_str;
long mode_bits;
SCM port;
@@ -366,20 +367,21 @@ ioscm_make_gdb_stdio_port (int fd)
{
case 0:
name = input_port_name;
- mode_bits = scm_mode_bits (is_a_tty ? "r0" : "r");
+ mode_str = is_a_tty ? "r0" : "r";
break;
case 1:
name = output_port_name;
- mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
+ mode_str = is_a_tty ? "w0" : "w";
break;
case 2:
name = error_port_name;
- mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
+ mode_str = is_a_tty ? "w0" : "w";
break;
default:
gdb_assert_not_reached ("bad stdio file descriptor");
}
+ mode_bits = scm_mode_bits ((char *) mode_str);
port = ioscm_open_port (stdio_port_desc, mode_bits);
scm_set_port_filename_x (port, gdbscm_scm_from_c_string (name));
--
1.9.3