From e6674512e3152706b86dd46b8a7a119430d446e2 Mon Sep 17 00:00:00 2001 From: Xerbo Date: Wed, 24 Jun 2020 15:55:04 +0100 Subject: [PATCH] Improve syncing, fix segfault Lack of clamping while reading a pixel from a palette resulted in reading invalid memory --- dsp.c | 9 ++++++++- pngio.c | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/dsp.c b/dsp.c index d5c3467..e1b2906 100755 --- a/dsp.c +++ b/dsp.c @@ -209,7 +209,6 @@ int getpixelv(float *pvbuff, int count) { } // Get an entire row of pixels, aligned with sync markers -// FIXME: loses sync easily int getpixelrow(float *pixelv, int nrow, int *zenith, int reset) { static float pixels[PixelLine + SyncFilterLen]; static int npv; @@ -256,6 +255,7 @@ int getpixelrow(float *pixelv, int nrow, int *zenith, int reset) { if (synced < 8) { int mshift; + static int lastmshift; if (npv < PixelLine + SyncFilterLen) { res = getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv); @@ -276,6 +276,13 @@ int getpixelrow(float *pixelv, int nrow, int *zenith, int reset) { } } + // Stop rows dissapearing into the void + int mshiftOrig = mshift; + if(abs(lastmshift-mshift) > 3 && nrow != 0){ + mshift = 0; + } + lastmshift = mshiftOrig; + // If we are already as aligned as we can get, just continue if (mshift == 0) { synced++; diff --git a/pngio.c b/pngio.c index df916e9..1365ca2 100644 --- a/pngio.c +++ b/pngio.c @@ -294,8 +294,8 @@ int applyUserPalette(float **prow, int nrow, char *filename, rgb_t **crow){ for(int y = 0; y < nrow; y++){ for(int x = 0; x < CH_WIDTH; x++){ - int cha = prow[y][x + CHA_OFFSET]; - int chb = prow[y][x + CHB_OFFSET]; + int cha = CLIP(prow[y][x + CHA_OFFSET], 0, 255); + int chb = CLIP(prow[y][x + CHB_OFFSET], 0, 255); crow[y][x + CHA_OFFSET] = pal_row[chb][cha]; } }