@@ -125,20 +125,50 @@ static char *gen_hist_bar(char *hist_bar,
125
125
unsigned short used_col )
126
126
{
127
127
#if defined(_WIN32 )
128
- size_t v = insn_freq * (max_col - used_col ) / max_insn_freq ;
128
+ size_t v =
129
+ max_insn_freq ? insn_freq * (max_col - used_col ) / max_insn_freq : 0 ;
129
130
for (size_t i = 0 ; i < v ; i ++ ) {
130
131
hist_bar [i ] = '*' ;
131
132
}
132
133
hist_bar [v ] = 0 ;
133
134
#else
134
135
const char * a [] = {" " , "▏" , "▎" , "▍" , "▌" , "▋" , "▊" , "▉" , "█" };
135
- size_t v = insn_freq * (max_col - used_col ) * 8 / max_insn_freq ;
136
- hist_bar [0 ] = '\0' ;
137
- while (v > 8 ) {
138
- strncat (hist_bar , a [8 ], hist_bar_len -- );
139
- v -= 8 ;
136
+ size_t units = max_insn_freq
137
+ ? insn_freq * (max_col - used_col ) * 8 / max_insn_freq
138
+ : 0 ;
139
+ size_t full = units / 8 ; /* count of full blocks */
140
+ size_t rem = units % 8 ; /* remainder block index */
141
+
142
+ char * p = hist_bar ;
143
+ size_t remaining = hist_bar_len ;
144
+
145
+ if (remaining == 0 )
146
+ return hist_bar ;
147
+
148
+ /* Append full blocks safely */
149
+ for (size_t i = 0 ; i < full ; i ++ ) {
150
+ const char * blk = a [8 ];
151
+ size_t glyph_len = strlen (blk ); /* UTF-8, typically 3 bytes */
152
+ if (glyph_len + 1 > remaining )
153
+ break ; /* not enough space for this glyph + NUL */
154
+ memcpy (p , blk , glyph_len );
155
+ p += glyph_len ;
156
+ remaining -= glyph_len ;
140
157
}
141
- strncat (hist_bar , a [v ], hist_bar_len -- );
158
+
159
+ /* Append remainder block if any */
160
+ if (rem > 0 ) {
161
+ const char * blk = a [rem ];
162
+ size_t glyph_len = strlen (blk );
163
+ if (glyph_len + 1 <= remaining ) {
164
+ memcpy (p , blk , glyph_len );
165
+ p += glyph_len ;
166
+ remaining -= glyph_len ;
167
+ }
168
+ }
169
+
170
+ /* NUL-terminate */
171
+ * p = '\0' ;
142
172
#endif
143
173
144
174
return hist_bar ;
0 commit comments