Fix a couple of valgrind-spotted out-of-bounds accesses that may have been causing segfaults, especially when opening the crossfade editor dialog. Some minor cleanups. Add some comments.

git-svn-id: svn://localhost/ardour2/branches/3.0@5020 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Carl Hetherington 2009-04-30 18:45:45 +00:00
parent 62fe887e24
commit c724b85839
2 changed files with 51 additions and 38 deletions

View file

@ -392,6 +392,7 @@ gnome_canvas_waveview_destroy (GtkObject *object)
#define DEBUG_CACHE 0 #define DEBUG_CACHE 0
#undef CACHE_MEMMOVE_OPTIMIZATION #undef CACHE_MEMMOVE_OPTIMIZATION
/** @return cache index of start_sample within the cache */
static gint32 static gint32
gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview, gulong start_sample, gulong end_sample) gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview, gulong start_sample, gulong end_sample)
{ {
@ -1091,8 +1092,7 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
int pymin, pymax; int pymin, pymax;
int cache_index; int cache_index;
double half_height; double half_height;
int x, end, begin; int x;
int zbegin, zend;
char rectify; char rectify;
waveview = GNOME_CANVAS_WAVEVIEW (item); waveview = GNOME_CANVAS_WAVEVIEW (item);
@ -1108,31 +1108,29 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
buf->is_bg = FALSE; buf->is_bg = FALSE;
} }
begin = MAX(waveview->bbox_ulx, buf->rect.x0); /* a "unit" means a pixel */
if (begin == waveview->bbox_ulx) { /* begin: render start x (units) */
zbegin = begin + 1; int const begin = MAX (waveview->bbox_ulx, buf->rect.x0);
} else {
zbegin = begin;
}
if (waveview->bbox_lrx >= 0) { /* zbegin: start x for zero line (units) */
end = MIN(waveview->bbox_lrx,buf->rect.x1); int const zbegin = (begin == waveview->bbox_ulx) ? (begin + 1) : begin;
} else {
end = buf->rect.x1;
}
if (end == waveview->bbox_lrx) { /* end: render end x (units) */
zend = end - 1; int const end = (waveview->bbox_lrx >= 0) ? MIN (waveview->bbox_lrx,buf->rect.x1) : buf->rect.x1;
} else {
zend = end; /* zend: end x for zero-line (units) */
} int const zend = (end == waveview->bbox_lrx) ? (end - 1) : end;
if (begin == end) { if (begin == end) {
return; return;
} }
s1 = floor ((begin - waveview->bbox_ulx) * waveview->samples_per_unit) ; /* s1: start sample
s2: end sample
*/
s1 = floor ((begin - waveview->bbox_ulx) * waveview->samples_per_unit);
// fprintf (stderr, "0x%x begins at sample %f\n", waveview, waveview->bbox_ulx * waveview->samples_per_unit); // fprintf (stderr, "0x%x begins at sample %f\n", waveview, waveview->bbox_ulx * waveview->samples_per_unit);
@ -1176,7 +1174,7 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
// check_cache (waveview, "post-ensure"); // check_cache (waveview, "post-ensure");
/* don't rectify at single-sample zoom */ /* don't rectify at single-sample zoom */
if(waveview->rectified && waveview->samples_per_unit > 1) { if (waveview->rectified && waveview->samples_per_unit > 1) {
rectify = TRUE; rectify = TRUE;
} }
else { else {
@ -1195,7 +1193,7 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
/* this makes it slightly easier to comprehend whats going on */ /* this makes it slightly easier to comprehend whats going on */
#define origin half_height #define origin half_height
if(waveview->filled && !rectify) { if (waveview->filled && !rectify) {
int prev_pymin = 1; int prev_pymin = 1;
int prev_pymax = 0; int prev_pymax = 0;
int last_pymin = 1; int last_pymin = 1;
@ -1205,7 +1203,7 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
int next_clip_max = 0; int next_clip_max = 0;
int next_clip_min = 0; int next_clip_min = 0;
if(s1 < waveview->samples_per_unit) { if (s1 < waveview->samples_per_unit) {
/* we haven't got a prev vars to compare with, so outline the whole line here */ /* we haven't got a prev vars to compare with, so outline the whole line here */
prev_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit); prev_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
prev_pymin = prev_pymax; prev_pymin = prev_pymax;
@ -1229,6 +1227,7 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
* Compute the variables outside the rendering rect * Compute the variables outside the rendering rect
*/ */
if(prev_pymax != prev_pymin) { if(prev_pymax != prev_pymin) {
prev_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[cache_index].max, 1.0) * half_height) * item->canvas->pixels_per_unit); prev_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[cache_index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
prev_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[cache_index].min, -1.0) * half_height) * item->canvas->pixels_per_unit); prev_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[cache_index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
++cache_index; ++cache_index;
@ -1237,8 +1236,20 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
/* take the index of one sample right of what we render */ /* take the index of one sample right of what we render */
int index = cache_index + (end - begin); int index = cache_index + (end - begin);
if (index >= waveview->cache->data_size) {
/* the data we want is off the end of the cache, which must mean its beyond
the end of the region's source; hence the peak values are 0 */
last_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
last_pymin = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
} else {
last_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[index].max, 1.0) * half_height) * item->canvas->pixels_per_unit); last_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
last_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[index].min, -1.0) * half_height) * item->canvas->pixels_per_unit); last_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
}
} }
/* /*
@ -1283,8 +1294,12 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
else { else {
++cache_index; ++cache_index;
if (cache_index < waveview->cache->data_size) {
max = waveview->cache->data[cache_index].max; max = waveview->cache->data[cache_index].max;
min = waveview->cache->data[cache_index].min; min = waveview->cache->data[cache_index].min;
} else {
max = min = 0;
}
next_clip_max = 0; next_clip_max = 0;
next_clip_min = 0; next_clip_min = 0;
@ -1611,7 +1626,6 @@ gnome_canvas_waveview_draw (GnomeCanvasItem *item,
cairo_t* cr; cairo_t* cr;
gulong s1, s2; gulong s1, s2;
int cache_index; int cache_index;
double zbegin, zend;
gboolean rectify; gboolean rectify;
double origin; double origin;
double clip_length; double clip_length;
@ -1630,10 +1644,8 @@ gnome_canvas_waveview_draw (GnomeCanvasItem *item,
if (x > waveview->bbox_ulx) { if (x > waveview->bbox_ulx) {
ulx = x; ulx = x;
zbegin = ulx;
} else { } else {
ulx = waveview->bbox_ulx; ulx = waveview->bbox_ulx;
zbegin = ulx + 1;
} }
if (y > waveview->bbox_uly) { if (y > waveview->bbox_uly) {
@ -1644,10 +1656,8 @@ gnome_canvas_waveview_draw (GnomeCanvasItem *item,
if (x + width > waveview->bbox_lrx) { if (x + width > waveview->bbox_lrx) {
lrx = waveview->bbox_lrx; lrx = waveview->bbox_lrx;
zend = lrx - 1;
} else { } else {
lrx = x + width; lrx = x + width;
zend = lrx;
} }
if (y + height > waveview->bbox_lry) { if (y + height > waveview->bbox_lry) {
@ -1675,8 +1685,6 @@ gnome_canvas_waveview_draw (GnomeCanvasItem *item,
uly -= y; uly -= y;
lrx -= x; lrx -= x;
lry -= y; lry -= y;
zbegin -= x;
zend -= x;
/* don't rectify at single-sample zoom */ /* don't rectify at single-sample zoom */
if(waveview->rectified && waveview->samples_per_unit > 1.0) { if(waveview->rectified && waveview->samples_per_unit > 1.0) {

View file

@ -283,6 +283,10 @@ AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, sframes_t start, nfr
return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP); return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
} }
/** @param peaks Buffer to write peak data.
* @param npeaks Number of peaks to write.
*/
int int
AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt,
double samples_per_visual_peak, nframes_t samples_per_file_peak) const double samples_per_visual_peak, nframes_t samples_per_file_peak) const
@ -544,6 +548,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t s
and fix it rather than do this band-aid move. and fix it rather than do this band-aid move.
*/ */
zero_fill = npeaks - nvisual_peaks; zero_fill = npeaks - nvisual_peaks;
npeaks -= zero_fill;
break; break;
} }