From 8c2c0a1d2fd18eca8183e47d08c17e1133cfefb0 Mon Sep 17 00:00:00 2001 From: Bruce Evans Date: Sun, 18 Jan 1998 13:18:55 +0000 Subject: [PATCH] Fixed endless loop for `p/x *(int *)0xf0000000'. kvm_uread() in gdb was cloned from the buggy version of kvm_uread() in libkvm and had the same bugs. It looped endlessly on EOF and checked errno without setting it in the lseek() error check. The first bug caused gdb to loop endlessly for reads from addresses between the end of the user area and the start of the kernel text. kvm_uread() should not be used for addresses beyond the end of the user area, but is due to bugs elsewhere. --- gnu/usr.bin/binutils/gdb/i386/kvm-fbsd.c | 17 ++++++++++------- gnu/usr.bin/binutils/gdb/kvm-fbsd.c | 17 ++++++++++------- gnu/usr.bin/gdb/gdb/kvm-fbsd.c | 17 ++++++++++------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/gnu/usr.bin/binutils/gdb/i386/kvm-fbsd.c b/gnu/usr.bin/binutils/gdb/i386/kvm-fbsd.c index 8004a38059e..5cb031134c6 100644 --- a/gnu/usr.bin/binutils/gdb/i386/kvm-fbsd.c +++ b/gnu/usr.bin/binutils/gdb/i386/kvm-fbsd.c @@ -578,11 +578,8 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) if (devmem) { - cp = myaddr; - sprintf (procfile, "/proc/%d/mem", p->p_pid); fd = open (procfile, O_RDONLY, 0); - if (fd < 0) { error ("cannot open %s", procfile); @@ -590,12 +587,13 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) return (0); } + cp = myaddr; while (len > 0) { - if (lseek (fd, memaddr, 0) == -1 && errno != 0) + errno = 0; + if (lseek (fd, (off_t)memaddr, 0) == -1 && errno != 0) { - error ("invalid address (%x) in %s", - memaddr, procfile); + error ("invalid address (%x) in %s", memaddr, procfile); break; } amount = read (fd, cp, len); @@ -604,13 +602,18 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) error ("error reading %s", procfile); break; } + if (amount == 0) + { + error ("EOF reading %s", procfile); + break; + } cp += amount; memaddr += amount; len -= amount; } close (fd); - return (ssize_t) (cp - myaddr); + return ((ssize_t) (cp - myaddr)); } else return (kernel_core_file_hook (core_kd, memaddr, myaddr, len)); diff --git a/gnu/usr.bin/binutils/gdb/kvm-fbsd.c b/gnu/usr.bin/binutils/gdb/kvm-fbsd.c index 8004a38059e..5cb031134c6 100644 --- a/gnu/usr.bin/binutils/gdb/kvm-fbsd.c +++ b/gnu/usr.bin/binutils/gdb/kvm-fbsd.c @@ -578,11 +578,8 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) if (devmem) { - cp = myaddr; - sprintf (procfile, "/proc/%d/mem", p->p_pid); fd = open (procfile, O_RDONLY, 0); - if (fd < 0) { error ("cannot open %s", procfile); @@ -590,12 +587,13 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) return (0); } + cp = myaddr; while (len > 0) { - if (lseek (fd, memaddr, 0) == -1 && errno != 0) + errno = 0; + if (lseek (fd, (off_t)memaddr, 0) == -1 && errno != 0) { - error ("invalid address (%x) in %s", - memaddr, procfile); + error ("invalid address (%x) in %s", memaddr, procfile); break; } amount = read (fd, cp, len); @@ -604,13 +602,18 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) error ("error reading %s", procfile); break; } + if (amount == 0) + { + error ("EOF reading %s", procfile); + break; + } cp += amount; memaddr += amount; len -= amount; } close (fd); - return (ssize_t) (cp - myaddr); + return ((ssize_t) (cp - myaddr)); } else return (kernel_core_file_hook (core_kd, memaddr, myaddr, len)); diff --git a/gnu/usr.bin/gdb/gdb/kvm-fbsd.c b/gnu/usr.bin/gdb/gdb/kvm-fbsd.c index 8004a38059e..5cb031134c6 100644 --- a/gnu/usr.bin/gdb/gdb/kvm-fbsd.c +++ b/gnu/usr.bin/gdb/gdb/kvm-fbsd.c @@ -578,11 +578,8 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) if (devmem) { - cp = myaddr; - sprintf (procfile, "/proc/%d/mem", p->p_pid); fd = open (procfile, O_RDONLY, 0); - if (fd < 0) { error ("cannot open %s", procfile); @@ -590,12 +587,13 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) return (0); } + cp = myaddr; while (len > 0) { - if (lseek (fd, memaddr, 0) == -1 && errno != 0) + errno = 0; + if (lseek (fd, (off_t)memaddr, 0) == -1 && errno != 0) { - error ("invalid address (%x) in %s", - memaddr, procfile); + error ("invalid address (%x) in %s", memaddr, procfile); break; } amount = read (fd, cp, len); @@ -604,13 +602,18 @@ kvm_uread (core_kd, p, memaddr, myaddr, len) error ("error reading %s", procfile); break; } + if (amount == 0) + { + error ("EOF reading %s", procfile); + break; + } cp += amount; memaddr += amount; len -= amount; } close (fd); - return (ssize_t) (cp - myaddr); + return ((ssize_t) (cp - myaddr)); } else return (kernel_core_file_hook (core_kd, memaddr, myaddr, len));