]> git.saurik.com Git - bison.git/blame_incremental - src/symtab.c
Fix a couple of buffer overrun bugs.
[bison.git] / src / symtab.c
... / ...
CommitLineData
1/* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <stdio.h>
22#include "system.h"
23#include "alloc.h"
24#include "symtab.h"
25#include "gram.h"
26
27
28bucket **symtab;
29bucket *firstsymbol;
30bucket *lastsymbol;
31
32
33
34int
35hash(key)
36char *key;
37{
38 register char *cp;
39 register int k;
40
41 cp = key;
42 k = 0;
43 while (*cp)
44 k = ((k << 1) ^ (*cp++)) & 0x3fff;
45
46 return (k % TABSIZE);
47}
48
49
50
51char *
52copys(s)
53char *s;
54{
55 register int i;
56 register char *cp;
57 register char *result;
58
59 i = 1;
60 for (cp = s; *cp; cp++)
61 i++;
62
63 result = xmalloc((unsigned int)i);
64 strcpy(result, s);
65 return (result);
66}
67
68
69void
70tabinit()
71{
72/* register int i; JF unused */
73
74 symtab = NEW2(TABSIZE, bucket *);
75
76 firstsymbol = NULL;
77 lastsymbol = NULL;
78}
79
80
81bucket *
82getsym(key)
83char *key;
84{
85 register int hashval;
86 register bucket *bp;
87 register int found;
88
89 hashval = hash(key);
90 bp = symtab[hashval];
91
92 found = 0;
93 while (bp != NULL && found == 0)
94 {
95 if (strcmp(key, bp->tag) == 0)
96 found = 1;
97 else
98 bp = bp->link;
99 }
100
101 if (found == 0)
102 {
103 nsyms++;
104
105 bp = NEW(bucket);
106 bp->link = symtab[hashval];
107 bp->next = NULL;
108 bp->tag = copys(key);
109 bp->class = SUNKNOWN;
110
111 if (firstsymbol == NULL)
112 {
113 firstsymbol = bp;
114 lastsymbol = bp;
115 }
116 else
117 {
118 lastsymbol->next = bp;
119 lastsymbol = bp;
120 }
121
122 symtab[hashval] = bp;
123 }
124
125 return (bp);
126}
127
128
129void
130free_symtab()
131{
132 register int i;
133 register bucket *bp,*bptmp;/* JF don't use ptr after free */
134
135 for (i = 0; i < TABSIZE; i++)
136 {
137 bp = symtab[i];
138 while (bp)
139 {
140 bptmp = bp->link;
141#if 0 /* This causes crashes because one string can appear more than once. */
142 if (bp->type_name)
143 FREE(bp->type_name);
144#endif
145 FREE(bp);
146 bp = bptmp;
147 }
148 }
149 FREE(symtab);
150}