Skip to content

Commit 4d1cd7c

Browse files
authored
Avoid UB in huffbench (#190)
The `--heap` results in an out of bounds pointer index. Fix this by subtracting 1 during indexing instead.
1 parent 7cd0c20 commit 4d1cd7c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

SingleSource/Benchmarks/CoyoteBench/huffbench.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,23 @@ static void heap_adjust(size_t * freq, size_t * heap, int n, int k)
111111
// queues and heaps for more explanation.
112112
int j;
113113

114-
--heap;
115-
116-
int v = heap[k];
114+
int v = heap[k-1];
117115

118116
while (k <= (n / 2))
119117
{
120118
j = k + k;
121119

122-
if ((j < n) && (freq[heap[j]] > freq[heap[j+1]]))
120+
if ((j < n) && (freq[heap[j-1]] > freq[heap[j]]))
123121
++j;
124122

125-
if (freq[v] < freq[heap[j]])
123+
if (freq[v] < freq[heap[j-1]])
126124
break;
127125

128-
heap[k] = heap[j];
126+
heap[k-1] = heap[j-1];
129127
k = j;
130128
}
131129

132-
heap[k] = v;
130+
heap[k-1] = v;
133131
}
134132

135133
// Huffman compression/decompression function

0 commit comments

Comments
 (0)