fix segfault due to limit of hashtable entries (global variables) discovered by Jb_boin: unlimit to 2^30 minus epsilon

This commit is contained in:
tg
2011-06-04 16:42:31 +00:00
parent b3d38f9cdf
commit ebb8bed911
3 changed files with 21 additions and 11 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.458 2011/06/04 16:11:16 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.459 2011/06/04 16:42:28 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -25,7 +25,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh # http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R40 2011/05/29 @(#)MIRBSD KSH R40 2011/06/04
description: description:
Check version of shell. Check version of shell.
stdin: stdin:

21
main.c
View File

@ -33,7 +33,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.191 2011/05/29 16:31:42 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.192 2011/06/04 16:42:30 tg Exp $");
extern char **environ; extern char **environ;
@ -1457,12 +1457,21 @@ texpand(struct table *tp, size_t nsize)
struct tbl *tblp, **pp; struct tbl *tblp, **pp;
struct tbl **ntblp, **otblp = tp->tbls; struct tbl **ntblp, **otblp = tp->tbls;
i = 1;
i <<= 30;
if (nsize > i)
internal_errorf("hash table size limit reached");
ntblp = alloc2(nsize, sizeof(struct tbl *), tp->areap); ntblp = alloc2(nsize, sizeof(struct tbl *), tp->areap);
for (i = 0; i < nsize; i++) memset(ntblp, 0, nsize * sizeof(struct tbl *));
ntblp[i] = NULL;
tp->size = nsize; tp->size = nsize;
/* table can get 80% full */ if (nsize == i) {
tp->nfree = (nsize * 4) / 5; /* cannot be grown any more, use a fixed value */
tp->nfree = i - 65536;
} else /* (nsize < 2^30) */ {
/* table can get 80% full */
tp->nfree = (nsize * 4) / 5;
}
tp->tbls = ntblp; tp->tbls = ntblp;
if (otblp == NULL) if (otblp == NULL)
return; return;
@ -1547,7 +1556,7 @@ ktenter(struct table *tp, const char *n, uint32_t h)
if ((p = ktscan(tp, n, h, &pp))) if ((p = ktscan(tp, n, h, &pp)))
return (p); return (p);
if (tp->nfree <= 0) { if (tp->nfree == 0) {
/* too full */ /* too full */
texpand(tp, 2 * tp->size); texpand(tp, 2 * tp->size);
goto Search; goto Search;

7
sh.h
View File

@ -151,9 +151,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.472 2011/06/04 16:11:19 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.473 2011/06/04 16:42:31 tg Exp $");
#endif #endif
#define MKSH_VERSION "R40 2011/05/29" #define MKSH_VERSION "R40 2011/06/04"
#ifndef MKSH_INCLUDES_ONLY #ifndef MKSH_INCLUDES_ONLY
@ -935,7 +935,8 @@ extern struct shf shf_iob[];
struct table { struct table {
Area *areap; /* area to allocate entries */ Area *areap; /* area to allocate entries */
struct tbl **tbls; /* hashed table items */ struct tbl **tbls; /* hashed table items */
short size, nfree; /* hash size (always 2^^n), free entries */ uint32_t size; /* table size (always 2^n) */
uint32_t nfree; /* free table entries */
}; };
struct tbl { /* table item */ struct tbl { /* table item */