summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2015-06-24 20:51:48 +0000
committerjilles <jilles@FreeBSD.org>2015-06-24 20:51:48 +0000
commit3c6c0527e691b4906e8ad2527fb2bfd2a8782cc1 (patch)
treebfc73dd1014492df563dc7bc078592da9eac68b2 /bin
parent70cda65ad974bbd25e7cb9eb77b9026933d83842 (diff)
downloadFreeBSD-src-3c6c0527e691b4906e8ad2527fb2bfd2a8782cc1.zip
FreeBSD-src-3c6c0527e691b4906e8ad2527fb2bfd2a8782cc1.tar.gz
sh: Fix some arithmetic undefined behaviour.
Fix shifts of possibly negative numbers found with ubsan and avoid signed integer overflow when hashing an extremely long command name. MFC after: 1 week
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/alias.c2
-rw-r--r--bin/sh/exec.c5
2 files changed, 3 insertions, 4 deletions
diff --git a/bin/sh/alias.c b/bin/sh/alias.c
index 21f3f44..a77ce99 100644
--- a/bin/sh/alias.c
+++ b/bin/sh/alias.c
@@ -248,7 +248,7 @@ hashalias(const char *p)
{
unsigned int hashval;
- hashval = *p << 4;
+ hashval = (unsigned char)*p << 4;
while (*p)
hashval+= *p++;
return &atab[hashval % ATABSIZE];
diff --git a/bin/sh/exec.c b/bin/sh/exec.c
index c9d4126..7d18c3c 100644
--- a/bin/sh/exec.c
+++ b/bin/sh/exec.c
@@ -522,17 +522,16 @@ static struct tblentry **lastcmdentry;
static struct tblentry *
cmdlookup(const char *name, int add)
{
- int hashval;
+ unsigned int hashval;
const char *p;
struct tblentry *cmdp;
struct tblentry **pp;
size_t len;
p = name;
- hashval = *p << 4;
+ hashval = (unsigned char)*p << 4;
while (*p)
hashval += *p++;
- hashval &= 0x7FFF;
pp = &cmdtable[hashval % CMDTABLESIZE];
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
if (equal(cmdp->cmdname, name))
OpenPOWER on IntegriCloud