Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Shaun Jackman <sjackman@gmail.com>
To: gdb-patches@sources.redhat.com
Subject: Re: sim/arm/armos.c: IsTTY [PATCH]
Date: Wed, 07 Sep 2005 16:51:00 -0000	[thread overview]
Message-ID: <7f45d93905090709516f912861@mail.gmail.com> (raw)
In-Reply-To: <20050830023718.GB16189@nevyn.them.org>

[-- Attachment #1: Type: text/plain, Size: 5407 bytes --]

2005/8/29, Daniel Jacobowitz <drow@false.org>:
> Please no buffer overflows be adding.  Also, there's plenty of ways to
> write this without the ugly empty loop body...

I fixed the potential buffer overflow and cleaned up the loop coding
style. Thanks for the tips.

Cheers,
Shaun

2005-09-07  Shaun Jackman  <sjackman@gmail.com>

	* sim/arm/armos.c (unlink): Remove this macro. It is unused
	in this file and conflicts with sim_callback->unlink.
	(SWIopen): Fix a potential buffer overflow.
	(SWIremove): New function.
	(SWIrename): Ditto.
	(ARMul_OSHandleSWI): Handle the RDP calls SWI_IsTTY,
	SWI_Remove, and SWI_Rename, as well as the RDI calls
	AngelSWI_Reason_IsTTY, AngelSWI_Reason_Remove, and
	AngelSWI_Reason_Rename.

Index: sim/arm/armos.c
===================================================================
RCS file: /cvs/src/src/sim/arm/armos.c,v
retrieving revision 1.22
diff -u -r1.22 armos.c
--- sim/arm/armos.c	12 May 2005 07:36:58 -0000	1.22
+++ sim/arm/armos.c	7 Sep 2005 16:45:27 -0000
@@ -27,6 +27,7 @@
 
  #include <time.h>
  #include <errno.h>
+#include <limits.h>
  #include <string.h>
  #include "targ-vals.h"
 
@@ -34,10 +35,6 @@
  #define TARGET_O_BINARY 0
  #endif
 
-#ifdef __STDC__
-#define unlink(s) remove(s)
-#endif
-
  #ifdef HAVE_UNISTD_H
  #include <unistd.h>		/* For SEEK_SET etc.  */
  #endif
@@ -303,18 +300,19 @@
  SWIopen (ARMul_State * state, ARMword name, ARMword SWIflags)
 {
   struct OSblock *OSptr = (struct OSblock *) state->OSptr;
-  char dummy[2000];
+  char buf[PATH_MAX], *p = buf;
   int flags;
   int i;
 
-  for (i = 0; (dummy[i] = ARMul_SafeReadByte (state, name + i)); i++)
-    ;
+  for (i = 0; i < sizeof buf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, name++)) == '\0')
+      break;
 
   /* Now we need to decode the Demon open mode.  */
   flags = translate_open_mode[SWIflags];
 
   /* Filename ":tt" is special: it denotes stdin/out.  */
-  if (strcmp (dummy, ":tt") == 0)
+  if (strcmp (buf, ":tt") == 0)
     {
       if (flags == TARGET_O_RDONLY) /* opening tty "r" */
  	state->Reg[0] = 0;	/* stdin */
@@ -323,7 +321,7 @@
     }
   else
     {
-      state->Reg[0] = sim_callback->open (sim_callback, dummy, flags);
+      state->Reg[0] = sim_callback->open (sim_callback, buf, flags);
       OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
     }
 }
@@ -403,6 +401,39 @@
   OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
 }
 
+static void
+SWIremove (ARMul_State * state, ARMword path)
+{
+  struct OSblock *OSptr = (struct OSblock *) state->OSptr;
+  char buf[PATH_MAX], *p = buf;
+  int i;
+
+  for (i = 0; i < sizeof buf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, path++)) == '\0')
+      break;
+
+  state->Reg[0] = sim_callback->unlink (sim_callback, buf);
+  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+}
+
+static void
+SWIrename (ARMul_State * state, ARMword old, ARMword new)
+{
+  struct OSblock *OSptr = (struct OSblock *) state->OSptr;
+  char oldbuf[PATH_MAX], newbuf[PATH_MAX], *p;
+  int i;
+
+  for (p = oldbuf, i = 0; i < sizeof oldbuf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, old++)) == '\0')
+      break;
+  for (p = newbuf, i = 0; i < sizeof newbuf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, new++)) == '\0')
+      break;
+
+  state->Reg[0] = sim_callback->rename (sim_callback, oldbuf, newbuf);
+  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+}
+
  /* The emulator calls this routine when a SWI instruction is encuntered.
    The parameter passed is the SWI number (lower 24 bits of the
instruction).  */
 
@@ -544,6 +575,30 @@
       state->Emulate = FALSE;
       break;
 
+    case SWI_Remove:
+      if (swi_mask & SWI_MASK_DEMON)
+	SWIremove (state, state->Reg[0]);
+      else
+	unhandled = TRUE;
+      break;
+
+    case SWI_Rename:
+      if (swi_mask & SWI_MASK_DEMON)
+	SWIrename (state, state->Reg[0], state->Reg[1]);
+      else
+	unhandled = TRUE;
+      break;
+
+    case SWI_IsTTY:
+      if (swi_mask & SWI_MASK_DEMON)
+	{
+	  state->Reg[0] = sim_callback->isatty (sim_callback, state->Reg[0]);
+	  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+	}
+      else
+	unhandled = TRUE;
+      break;
+
       /* Handle Angel SWIs as well as Demon ones.  */
     case AngelSWI_ARM:
     case AngelSWI_Thumb:
@@ -566,10 +621,7 @@
 	  
  	      /* Unimplemented reason codes.  */
 	    case AngelSWI_Reason_ReadC:
-	    case AngelSWI_Reason_IsTTY:
 	    case AngelSWI_Reason_TmpNam:
-	    case AngelSWI_Reason_Remove:
-	    case AngelSWI_Reason_Rename:
 	    case AngelSWI_Reason_System:
 	    case AngelSWI_Reason_EnterSVC:
  	    default:
@@ -684,6 +736,21 @@
  			ARMul_ReadWord (state, addr + 4),
  			ARMul_ReadWord (state, addr + 8));
 	      break;
+
+	    case AngelSWI_Reason_IsTTY:
+	      state->Reg[0] = sim_callback->close (sim_callback,
+						   ARMul_ReadWord (state, addr));
+	      OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+	      break;
+
+	    case AngelSWI_Reason_Remove:
+	      SWIremove (state,
+			 ARMul_ReadWord (state, addr));
+
+	    case AngelSWI_Reason_Rename:
+	      SWIrename (state,
+			 ARMul_ReadWord (state, addr),
+			 ARMul_ReadWord (state, addr + 4));
 	    }
 	}
       else

[-- Attachment #2: sim-arm-swi.diff --]
[-- Type: text/plain, Size: 4922 bytes --]

2005-09-07  Shaun Jackman  <sjackman@gmail.com>

	* sim/arm/armos.c (unlink): Remove this macro. It is unused
	in this file and conflicts with sim_callback->unlink.
	(SWIopen): Fix a potential buffer overflow.
	(SWIremove): New function.
	(SWIrename): Ditto.
	(ARMul_OSHandleSWI): Handle the RDP calls SWI_IsTTY,
	SWI_Remove, and SWI_Rename, as well as the RDI calls
	AngelSWI_Reason_IsTTY, AngelSWI_Reason_Remove, and
	AngelSWI_Reason_Rename.

Index: sim/arm/armos.c
===================================================================
RCS file: /cvs/src/src/sim/arm/armos.c,v
retrieving revision 1.22
diff -u -r1.22 armos.c
--- sim/arm/armos.c	12 May 2005 07:36:58 -0000	1.22
+++ sim/arm/armos.c	7 Sep 2005 16:45:27 -0000
@@ -27,6 +27,7 @@
 
 #include <time.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include "targ-vals.h"
 
@@ -34,10 +35,6 @@
 #define TARGET_O_BINARY 0
 #endif
 
-#ifdef __STDC__
-#define unlink(s) remove(s)
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>		/* For SEEK_SET etc.  */
 #endif
@@ -303,18 +300,19 @@
 SWIopen (ARMul_State * state, ARMword name, ARMword SWIflags)
 {
   struct OSblock *OSptr = (struct OSblock *) state->OSptr;
-  char dummy[2000];
+  char buf[PATH_MAX], *p = buf;
   int flags;
   int i;
 
-  for (i = 0; (dummy[i] = ARMul_SafeReadByte (state, name + i)); i++)
-    ;
+  for (i = 0; i < sizeof buf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, name++)) == '\0')
+      break;
 
   /* Now we need to decode the Demon open mode.  */
   flags = translate_open_mode[SWIflags];
 
   /* Filename ":tt" is special: it denotes stdin/out.  */
-  if (strcmp (dummy, ":tt") == 0)
+  if (strcmp (buf, ":tt") == 0)
     {
       if (flags == TARGET_O_RDONLY) /* opening tty "r" */
 	state->Reg[0] = 0;	/* stdin */
@@ -323,7 +321,7 @@
     }
   else
     {
-      state->Reg[0] = sim_callback->open (sim_callback, dummy, flags);
+      state->Reg[0] = sim_callback->open (sim_callback, buf, flags);
       OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
     }
 }
@@ -403,6 +401,39 @@
   OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
 }
 
+static void
+SWIremove (ARMul_State * state, ARMword path)
+{
+  struct OSblock *OSptr = (struct OSblock *) state->OSptr;
+  char buf[PATH_MAX], *p = buf;
+  int i;
+
+  for (i = 0; i < sizeof buf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, path++)) == '\0')
+      break;
+
+  state->Reg[0] = sim_callback->unlink (sim_callback, buf);
+  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+}
+
+static void
+SWIrename (ARMul_State * state, ARMword old, ARMword new)
+{
+  struct OSblock *OSptr = (struct OSblock *) state->OSptr;
+  char oldbuf[PATH_MAX], newbuf[PATH_MAX], *p;
+  int i;
+
+  for (p = oldbuf, i = 0; i < sizeof oldbuf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, old++)) == '\0')
+      break;
+  for (p = newbuf, i = 0; i < sizeof newbuf; i++)
+    if ((*p++ = ARMul_SafeReadByte (state, new++)) == '\0')
+      break;
+
+  state->Reg[0] = sim_callback->rename (sim_callback, oldbuf, newbuf);
+  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+}
+
 /* The emulator calls this routine when a SWI instruction is encuntered.
    The parameter passed is the SWI number (lower 24 bits of the instruction).  */
 
@@ -544,6 +575,30 @@
       state->Emulate = FALSE;
       break;
 
+    case SWI_Remove:
+      if (swi_mask & SWI_MASK_DEMON)
+	SWIremove (state, state->Reg[0]);
+      else
+	unhandled = TRUE;
+      break;
+
+    case SWI_Rename:
+      if (swi_mask & SWI_MASK_DEMON)
+	SWIrename (state, state->Reg[0], state->Reg[1]);
+      else
+	unhandled = TRUE;
+      break;
+
+    case SWI_IsTTY:
+      if (swi_mask & SWI_MASK_DEMON)
+	{
+	  state->Reg[0] = sim_callback->isatty (sim_callback, state->Reg[0]);
+	  OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+	}
+      else
+	unhandled = TRUE;
+      break;
+
       /* Handle Angel SWIs as well as Demon ones.  */
     case AngelSWI_ARM:
     case AngelSWI_Thumb:
@@ -566,10 +621,7 @@
 	  
 	      /* Unimplemented reason codes.  */
 	    case AngelSWI_Reason_ReadC:
-	    case AngelSWI_Reason_IsTTY:
 	    case AngelSWI_Reason_TmpNam:
-	    case AngelSWI_Reason_Remove:
-	    case AngelSWI_Reason_Rename:
 	    case AngelSWI_Reason_System:
 	    case AngelSWI_Reason_EnterSVC:
 	    default:
@@ -684,6 +736,21 @@
 			ARMul_ReadWord (state, addr + 4),
 			ARMul_ReadWord (state, addr + 8));
 	      break;
+
+	    case AngelSWI_Reason_IsTTY:
+	      state->Reg[0] = sim_callback->close (sim_callback,
+						   ARMul_ReadWord (state, addr));
+	      OSptr->ErrorNo = sim_callback->get_errno (sim_callback);
+	      break;
+
+	    case AngelSWI_Reason_Remove:
+	      SWIremove (state,
+			 ARMul_ReadWord (state, addr));
+
+	    case AngelSWI_Reason_Rename:
+	      SWIrename (state,
+			 ARMul_ReadWord (state, addr),
+			 ARMul_ReadWord (state, addr + 4));
 	    }
 	}
       else

  parent reply	other threads:[~2005-09-07 16:51 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-15 21:19 Shaun Jackman
2005-08-22 18:51 ` Shaun Jackman
2005-08-30  2:38 ` Daniel Jacobowitz
2005-09-06 16:22   ` Richard Earnshaw
2005-09-07 16:51   ` Shaun Jackman [this message]
2005-09-08  9:07     ` Richard Earnshaw
2005-09-08 19:28       ` Eli Zaretskii
2005-09-09 17:16       ` Shaun Jackman
2005-09-09 17:23       ` Shaun Jackman
2005-09-17 22:37         ` Daniel Jacobowitz
2005-09-17 23:32           ` Shaun Jackman
2005-09-18  1:14             ` Daniel Jacobowitz
2005-09-18  2:40               ` Shaun Jackman
2005-09-18  2:59                 ` Daniel Jacobowitz
     [not found]                 ` <20050918025653.GA4285@nevyn.them.org>
     [not found]                   ` <7f45d93905091720161f61e995@mail.gmail.com>
     [not found]                     ` <20050918033152.GA6546@nevyn.them.org>
2005-10-13 20:30                       ` Shaun Jackman
2005-10-19 16:12                 ` Shaun Jackman
2005-11-17 10:49                   ` Daniel Jacobowitz
2005-11-17 15:54                     ` Shaun Jackman
2005-09-18  3:32           ` Eli Zaretskii
2005-09-18  3:44             ` Daniel Jacobowitz
2005-09-18  9:04               ` Mark Kettenis
2005-09-18 19:13                 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f45d93905090709516f912861@mail.gmail.com \
    --to=sjackman@gmail.com \
    --cc=gdb-patches@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox