* Off-by-one error in windows-nat.c causes abort at startup
@ 2016-04-30 11:08 Eli Zaretskii
2016-05-02 11:50 ` Pedro Alves
0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2016-04-30 11:08 UTC (permalink / raw)
To: gdb-patches
I created a gdb.ini file in my home directory, and suddenly found that
almost all my GDB binaries stopped working. Even "gdb --version"
would crash at startup thusly:
./common/common-utils.c:141: internal-error: xsnprintf: Assertion `ret < size' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n) [answered Y; input not from terminal]
This is a bug, please report it. For instructions, see:
<http://www.gnu.org/software/gdb/bugs/>.
./common/common-utils.c:141: internal-error: xsnprintf: Assertion `ret < size' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Create a core file of GDB? (y or n) [answered Y; input not from terminal]
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Luckily, I still had GDB 7.5, which did work. Using it, I found the
off-by-one gotcha below (".gdbinit" is one character longer than
"gdb.ini"). I guess no one tested this feature when we switched from
using snprintf to xsnprintf...
OK to commit (with a suitable ChangeLog entry, of course)?
--- gdb/windows-nat.c~ 2016-02-10 05:19:39.000000000 +0200
+++ gdb/windows-nat.c 2016-04-30 11:57:08.500000000 +0300
@@ -2711,9 +2711,9 @@ _initialize_check_for_gdb_ini (void)
if (access (oldini, 0) == 0)
{
int len = strlen (oldini);
- char *newini = (char *) alloca (len + 1);
+ char *newini = (char *) alloca (len + 2);
- xsnprintf (newini, len + 1, "%.*s.gdbinit",
+ xsnprintf (newini, len + 2, "%.*s.gdbinit",
(int) (len - (sizeof ("gdb.ini") - 1)), oldini);
warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini);
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Off-by-one error in windows-nat.c causes abort at startup
2016-04-30 11:08 Off-by-one error in windows-nat.c causes abort at startup Eli Zaretskii
@ 2016-05-02 11:50 ` Pedro Alves
2016-05-02 16:40 ` Eli Zaretskii
0 siblings, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2016-05-02 11:50 UTC (permalink / raw)
To: Eli Zaretskii, gdb-patches
On 04/30/2016 12:07 PM, Eli Zaretskii wrote:
> Luckily, I still had GDB 7.5, which did work. Using it, I found the
> off-by-one gotcha below (".gdbinit" is one character longer than
> "gdb.ini"). I guess no one tested this feature when we switched from
> using snprintf to xsnprintf...
Sounds like gdb would corrupt memory before we switched to xsnprintf
then. I'd say the problem is that the feature was added without a
corresponding test case.
> OK to commit (with a suitable ChangeLog entry, of course)?
Sure.
>
> --- gdb/windows-nat.c~ 2016-02-10 05:19:39.000000000 +0200
> +++ gdb/windows-nat.c 2016-04-30 11:57:08.500000000 +0300
> @@ -2711,9 +2711,9 @@ _initialize_check_for_gdb_ini (void)
> if (access (oldini, 0) == 0)
> {
> int len = strlen (oldini);
> - char *newini = (char *) alloca (len + 1);
> + char *newini = (char *) alloca (len + 2);
>
> - xsnprintf (newini, len + 1, "%.*s.gdbinit",
> + xsnprintf (newini, len + 2, "%.*s.gdbinit",
> (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
> warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini);
(I suspect this whole function could be rewritten in a clearer form...)
Thanks,
Pedro Alves
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Off-by-one error in windows-nat.c causes abort at startup
2016-05-02 11:50 ` Pedro Alves
@ 2016-05-02 16:40 ` Eli Zaretskii
2016-05-02 18:01 ` Pedro Alves
0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2016-05-02 16:40 UTC (permalink / raw)
To: Pedro Alves; +Cc: gdb-patches
> From: Pedro Alves <palves@redhat.com>
> Date: Mon, 2 May 2016 12:50:05 +0100
>
> On 04/30/2016 12:07 PM, Eli Zaretskii wrote:
>
> > Luckily, I still had GDB 7.5, which did work. Using it, I found the
> > off-by-one gotcha below (".gdbinit" is one character longer than
> > "gdb.ini"). I guess no one tested this feature when we switched from
> > using snprintf to xsnprintf...
>
> Sounds like gdb would corrupt memory before we switched to xsnprintf
> then. I'd say the problem is that the feature was added without a
> corresponding test case.
>
> > OK to commit (with a suitable ChangeLog entry, of course)?
>
> Sure.
Thanks, pushed.
> > --- gdb/windows-nat.c~ 2016-02-10 05:19:39.000000000 +0200
> > +++ gdb/windows-nat.c 2016-04-30 11:57:08.500000000 +0300
> > @@ -2711,9 +2711,9 @@ _initialize_check_for_gdb_ini (void)
> > if (access (oldini, 0) == 0)
> > {
> > int len = strlen (oldini);
> > - char *newini = (char *) alloca (len + 1);
> > + char *newini = (char *) alloca (len + 2);
> >
> > - xsnprintf (newini, len + 1, "%.*s.gdbinit",
> > + xsnprintf (newini, len + 2, "%.*s.gdbinit",
> > (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
> > warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini);
>
> (I suspect this whole function could be rewritten in a clearer form...)
Like not use xsnprintf at all, and instead use strcpy/strcat, perhaps?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Off-by-one error in windows-nat.c causes abort at startup
2016-05-02 16:40 ` Eli Zaretskii
@ 2016-05-02 18:01 ` Pedro Alves
0 siblings, 0 replies; 4+ messages in thread
From: Pedro Alves @ 2016-05-02 18:01 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: gdb-patches
On 05/02/2016 05:39 PM, Eli Zaretskii wrote:
>> From: Pedro Alves <palves@redhat.com>
>>> --- gdb/windows-nat.c~ 2016-02-10 05:19:39.000000000 +0200
>>> +++ gdb/windows-nat.c 2016-04-30 11:57:08.500000000 +0300
>>> @@ -2711,9 +2711,9 @@ _initialize_check_for_gdb_ini (void)
>>> if (access (oldini, 0) == 0)
>>> {
>>> int len = strlen (oldini);
>>> - char *newini = (char *) alloca (len + 1);
>>> + char *newini = (char *) alloca (len + 2);
>>>
>>> - xsnprintf (newini, len + 1, "%.*s.gdbinit",
>>> + xsnprintf (newini, len + 2, "%.*s.gdbinit",
>>> (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
>>> warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini);
>>
>> (I suspect this whole function could be rewritten in a clearer form...)
>
> Like not use xsnprintf at all, and instead use strcpy/strcat, perhaps?
>
Yeah, something like this (untested).
From 33738f6c5bf7f76d6cb6853aecf880c5570f7d74 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Mon, 2 May 2016 18:57:50 +0100
Subject: [PATCH] Rewrite/simplify old gdb.ini warning
---
gdb/windows-nat.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 2e8a777..161e887 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2694,27 +2694,28 @@ void
_initialize_check_for_gdb_ini (void)
{
char *homedir;
+
if (inhibit_gdbinit)
return;
homedir = getenv ("HOME");
- if (homedir)
+ if (homedir != NULL)
{
- char *p;
- char *oldini = (char *) alloca (strlen (homedir) +
- sizeof ("gdb.ini") + 1);
- strcpy (oldini, homedir);
- p = strchr (oldini, '\0');
- if (p > oldini && !IS_DIR_SEPARATOR (p[-1]))
- *p++ = '/';
- strcpy (p, "gdb.ini");
+ size_t homedir_len = strlen (homedir);
+ char *oldini;
+
+ while (homedir_len > 0 && IS_DIR_SEPARATOR (homedir[homedir_len - 1]))
+ homedir_len--;
+
+ oldini = (char *) alloca (homedir_len + sizeof ("/gdb.ini"));
+ memcpy (oldini, homedir, homedir_len);
+ strcpy (oldini + homedir_len, "/gdb.ini");
if (access (oldini, 0) == 0)
{
- int len = strlen (oldini);
- char *newini = (char *) alloca (len + 2);
+ char *newini = (char *) alloca (homedir_len + sizeof ("/.gdbinit"));
- xsnprintf (newini, len + 2, "%.*s.gdbinit",
- (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
+ memcpy (newini, homedir, homedir_len);
+ strcpy (newini + homedir_len, "/.gdbinit");
warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini);
}
}
--
2.5.5
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-05-02 18:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-30 11:08 Off-by-one error in windows-nat.c causes abort at startup Eli Zaretskii
2016-05-02 11:50 ` Pedro Alves
2016-05-02 16:40 ` Eli Zaretskii
2016-05-02 18:01 ` Pedro Alves
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).