@@ -278,9 +278,21 @@ void streamGetEdgeID(stream *s, int first, int skip_tombstones, streamID *edge_i
278278 streamIteratorStop (& si );
279279}
280280
281- void checkListPackNotEmpty (unsigned char * lp ) {
281+ void checkListPackValid (unsigned char * lp ) {
282+ unsigned char * p = NULL ;
282283 if (lpBytes (lp ) == 0 ) {
283- fprintf (stderr , "Error: corrupted listpack found." );
284+ fprintf (stderr , "Error: corrupted listpack header found.\n" );
285+ abort ();
286+ }
287+ p = lpValidateFirst (lp );
288+ if (p == NULL ) {
289+ fprintf (stderr , "Error: empty listpack found.\n" );
290+ abort ();
291+ }
292+
293+ if (lpValidateNext (lp , & p , lpBytes (lp )) == 0 ) {
294+ lpRepr (lp );
295+ fprintf (stderr , "Error: corrupted listpack found.\n" );
284296 abort ();
285297 }
286298}
@@ -327,7 +339,11 @@ int64_t streamTrim(stream *s, streamAddTrimArgs *args) {
327339 if (trim_strategy == TRIM_STRATEGY_MAXLEN && s -> length <= maxlen )
328340 break ;
329341
330- unsigned char * lp = ri .data , * p = lpFirst (lp );
342+ unsigned char * lp = ri .data ;
343+ unsigned char * p ;
344+
345+ checkListPackValid (lp );
346+ p = lpFirst (lp );
331347 int64_t entries = lpGetInteger (p );
332348
333349 /* Check if we exceeded the amount of work we could do */
@@ -452,7 +468,7 @@ int64_t streamTrim(stream *s, streamAddTrimArgs *args) {
452468
453469 /* Update the listpack with the new pointer. */
454470 raxInsert (s -> rax_tree ,ri .key ,ri .key_len ,lp ,NULL );
455- checkListPackNotEmpty (lp );
471+ checkListPackValid (lp );
456472
457473 break ; /* If we are here, there was enough to delete in the current
458474 node, so no need to go to the next node. */
@@ -760,7 +776,7 @@ void streamIteratorRemoveEntry(streamIterator *si, streamID *current) {
760776 if (si -> lp != lp )
761777 raxInsert (si -> stream -> rax_tree ,si -> ri .key ,si -> ri .key_len ,lp ,NULL );
762778
763- checkListPackNotEmpty (lp );
779+ checkListPackValid (lp );
764780 }
765781
766782 /* Update the number of entries counter. */
0 commit comments