Commit a463d9bd authored by Laurent Aimar's avatar Laurent Aimar

Made stream_t size and position unsigned.

 It fixes segfaults in src/input/stream.c (at least) when demuxers
overflow int64_t seek position, and avoid testing for negative values
everywhere.
 stream_Tell() and stream_Size() still returns signed values as too much code
depend on it.
parent 2e847d84
...@@ -90,10 +90,10 @@ enum stream_query_e ...@@ -90,10 +90,10 @@ enum stream_query_e
STREAM_CAN_FASTSEEK, /**< arg1= bool * res=cannot fail*/ STREAM_CAN_FASTSEEK, /**< arg1= bool * res=cannot fail*/
/* */ /* */
STREAM_SET_POSITION, /**< arg1= int64_t res=can fail */ STREAM_SET_POSITION, /**< arg1= uint64_t res=can fail */
STREAM_GET_POSITION, /**< arg1= int64_t * res=cannot fail*/ STREAM_GET_POSITION, /**< arg1= uint64_t * res=cannot fail*/
STREAM_GET_SIZE, /**< arg1= int64_t * res=cannot fail (0 if no sense)*/ STREAM_GET_SIZE, /**< arg1= uint64_t * res=cannot fail (0 if no sense)*/
/* Special for direct access control from demuxer. /* Special for direct access control from demuxer.
* XXX: avoid using it by all means */ * XXX: avoid using it by all means */
...@@ -124,8 +124,10 @@ VLC_EXPORT( char *, stream_ReadLine, ( stream_t * ) ); ...@@ -124,8 +124,10 @@ VLC_EXPORT( char *, stream_ReadLine, ( stream_t * ) );
*/ */
static inline int64_t stream_Tell( stream_t *s ) static inline int64_t stream_Tell( stream_t *s )
{ {
int64_t i_pos; uint64_t i_pos;
stream_Control( s, STREAM_GET_POSITION, &i_pos ); stream_Control( s, STREAM_GET_POSITION, &i_pos );
if( i_pos >> 62 )
return (int64_t)1 << 62;
return i_pos; return i_pos;
} }
...@@ -134,12 +136,14 @@ static inline int64_t stream_Tell( stream_t *s ) ...@@ -134,12 +136,14 @@ static inline int64_t stream_Tell( stream_t *s )
*/ */
static inline int64_t stream_Size( stream_t *s ) static inline int64_t stream_Size( stream_t *s )
{ {
int64_t i_pos; uint64_t i_pos;
stream_Control( s, STREAM_GET_SIZE, &i_pos ); stream_Control( s, STREAM_GET_SIZE, &i_pos );
if( i_pos >> 62 )
return (int64_t)1 << 62;
return i_pos; return i_pos;
} }
static inline int stream_Seek( stream_t *s, int64_t i_pos ) static inline int stream_Seek( stream_t *s, uint64_t i_pos )
{ {
return stream_Control( s, STREAM_SET_POSITION, i_pos ); return stream_Control( s, STREAM_SET_POSITION, i_pos );
} }
...@@ -172,7 +176,7 @@ VLC_EXPORT( void, stream_DemuxSend, ( stream_t *s, block_t *p_block ) ); ...@@ -172,7 +176,7 @@ VLC_EXPORT( void, stream_DemuxSend, ( stream_t *s, block_t *p_block ) );
* You must delete it using stream_Delete. * You must delete it using stream_Delete.
*/ */
#define stream_MemoryNew( a, b, c, d ) __stream_MemoryNew( VLC_OBJECT(a), b, c, d ) #define stream_MemoryNew( a, b, c, d ) __stream_MemoryNew( VLC_OBJECT(a), b, c, d )
VLC_EXPORT( stream_t *,__stream_MemoryNew, (vlc_object_t *p_obj, uint8_t *p_buffer, int64_t i_size, bool b_preserve_memory ) ); VLC_EXPORT( stream_t *,__stream_MemoryNew, (vlc_object_t *p_obj, uint8_t *p_buffer, uint64_t i_size, bool b_preserve_memory ) );
/** /**
* Create a stream_t reading from an URL. * Create a stream_t reading from an URL.
......
...@@ -305,7 +305,7 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -305,7 +305,7 @@ static int Control( stream_t *s, int i_query, va_list args )
{ {
case STREAM_SET_POSITION: case STREAM_SET_POSITION:
{ {
int64_t i_position = (int64_t)va_arg( args, int64_t ); uint64_t i_position = va_arg( args, uint64_t );
if( i_position >= p_sys->i_len ) if( i_position >= p_sys->i_len )
return VLC_EGENERIC; return VLC_EGENERIC;
else else
...@@ -317,15 +317,15 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -317,15 +317,15 @@ static int Control( stream_t *s, int i_query, va_list args )
case STREAM_GET_POSITION: case STREAM_GET_POSITION:
{ {
int64_t *pi_position = (int64_t*)va_arg( args, int64_t* ); uint64_t *pi_position = va_arg( args, uint64_t* );
*pi_position = p_sys->i_pos; *pi_position = p_sys->i_pos;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
case STREAM_GET_SIZE: case STREAM_GET_SIZE:
{ {
int64_t *pi_size = (int64_t*)va_arg( args, int64_t* ); uint64_t *pi_size = va_arg( args, uint64_t* );
*pi_size = (int64_t) p_sys->i_len; *pi_size = p_sys->i_len;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
......
...@@ -243,10 +243,10 @@ static int Control (stream_t *stream, int query, va_list args) ...@@ -243,10 +243,10 @@ static int Control (stream_t *stream, int query, va_list args)
*(va_arg (args, bool *)) = false; *(va_arg (args, bool *)) = false;
break; break;
case STREAM_GET_POSITION: case STREAM_GET_POSITION:
*(va_arg (args, int64_t *)) = p_sys->offset; *(va_arg (args, uint64_t *)) = p_sys->offset;
break; break;
case STREAM_GET_SIZE: case STREAM_GET_SIZE:
*(va_arg (args, int64_t *)) = 0; *(va_arg (args, uint64_t *)) = 0;
break; break;
default: default:
return VLC_EGENERIC; return VLC_EGENERIC;
......
...@@ -59,19 +59,19 @@ static const int i_rar_marker = sizeof(p_rar_marker); ...@@ -59,19 +59,19 @@ static const int i_rar_marker = sizeof(p_rar_marker);
typedef struct typedef struct
{ {
int64_t i_offset; uint64_t i_offset;
int64_t i_size; uint64_t i_size;
int64_t i_cummulated_size; uint64_t i_cummulated_size;
} rar_file_chunk_t; } rar_file_chunk_t;
typedef struct typedef struct
{ {
char *psz_name; char *psz_name;
int64_t i_size; uint64_t i_size;
bool b_complete; bool b_complete;
int i_chunk; int i_chunk;
rar_file_chunk_t **pp_chunk; rar_file_chunk_t **pp_chunk;
int64_t i_real_size; /* Gathered size */ uint64_t i_real_size; /* Gathered size */
} rar_file_t; } rar_file_t;
static void RarFileDelete( rar_file_t * ); static void RarFileDelete( rar_file_t * );
...@@ -81,7 +81,7 @@ struct stream_sys_t ...@@ -81,7 +81,7 @@ struct stream_sys_t
rar_file_t *p_file; rar_file_t *p_file;
const rar_file_chunk_t *p_chunk; const rar_file_chunk_t *p_chunk;
int64_t i_position; uint64_t i_position;
uint8_t *p_peek_alloc; uint8_t *p_peek_alloc;
uint8_t *p_peek; uint8_t *p_peek;
...@@ -97,7 +97,7 @@ static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek ); ...@@ -97,7 +97,7 @@ static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
static int Control( stream_t *, int i_query, va_list ); static int Control( stream_t *, int i_query, va_list );
static int Parse ( stream_t * ); static int Parse ( stream_t * );
static int Seek ( stream_t *s, int64_t i_position ); static int Seek ( stream_t *s, uint64_t i_position );
/**************************************************************************** /****************************************************************************
* Open * Open
...@@ -192,7 +192,7 @@ static int Read( stream_t *s, void *p_read, unsigned int i_read ) ...@@ -192,7 +192,7 @@ static int Read( stream_t *s, void *p_read, unsigned int i_read )
while( i_total < i_read ) while( i_total < i_read )
{ {
const int64_t i_chunk_end = p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size; const uint64_t i_chunk_end = p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size;
int i_max = __MIN( i_read - i_total, i_chunk_end - p_sys->i_position ); int i_max = __MIN( i_read - i_total, i_chunk_end - p_sys->i_position );
if( i_max <= 0 ) if( i_max <= 0 )
...@@ -257,20 +257,20 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -257,20 +257,20 @@ static int Control( stream_t *s, int i_query, va_list args )
/* */ /* */
case STREAM_SET_POSITION: case STREAM_SET_POSITION:
{ {
int64_t i_position = (int64_t)va_arg( args, int64_t ); uint64_t i_position = va_arg( args, uint64_t );
return Seek( s, i_position ); return Seek( s, i_position );
} }
case STREAM_GET_POSITION: case STREAM_GET_POSITION:
{ {
int64_t *pi_position = (int64_t*)va_arg( args, int64_t* ); uint64_t *pi_position = va_arg( args, uint64_t* );
*pi_position = p_sys->i_position - p_sys->i_peek; *pi_position = p_sys->i_position - p_sys->i_peek;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
case STREAM_GET_SIZE: case STREAM_GET_SIZE:
{ {
int64_t *pi_size = (int64_t*)va_arg( args, int64_t* ); uint64_t *pi_size = (uint64_t*)va_arg( args, uint64_t* );
*pi_size = p_sys->p_file->i_real_size; *pi_size = p_sys->p_file->i_real_size;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -293,13 +293,11 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -293,13 +293,11 @@ static int Control( stream_t *s, int i_query, va_list args )
/**************************************************************************** /****************************************************************************
* Helpers * Helpers
****************************************************************************/ ****************************************************************************/
static int Seek( stream_t *s, int64_t i_position ) static int Seek( stream_t *s, uint64_t i_position )
{ {
stream_sys_t *p_sys = s->p_sys; stream_sys_t *p_sys = s->p_sys;
if( i_position < 0 ) if( i_position > p_sys->p_file->i_real_size )
i_position = 0;
else if( i_position > p_sys->p_file->i_real_size )
i_position = p_sys->p_file->i_real_size; i_position = p_sys->p_file->i_real_size;
/* Search the chunk */ /* Search the chunk */
...@@ -313,8 +311,8 @@ static int Seek( stream_t *s, int64_t i_position ) ...@@ -313,8 +311,8 @@ static int Seek( stream_t *s, int64_t i_position )
p_sys->i_position = i_position; p_sys->i_position = i_position;
p_sys->i_peek = 0; p_sys->i_peek = 0;
const int64_t i_seek = p_sys->p_chunk->i_offset + const uint64_t i_seek = p_sys->p_chunk->i_offset +
( i_position - p_sys->p_chunk->i_cummulated_size ); ( i_position - p_sys->p_chunk->i_cummulated_size );
return stream_Seek( s->p_source, i_seek ); return stream_Seek( s->p_source, i_seek );
} }
...@@ -380,9 +378,8 @@ static int PeekBlock( stream_t *s, rar_block_t *p_hdr ) ...@@ -380,9 +378,8 @@ static int PeekBlock( stream_t *s, rar_block_t *p_hdr )
} }
static int SkipBlock( stream_t *s, const rar_block_t *p_hdr ) static int SkipBlock( stream_t *s, const rar_block_t *p_hdr )
{ {
int64_t i_size = (int64_t)p_hdr->i_size + p_hdr->i_add_size; uint64_t i_size = (uint64_t)p_hdr->i_size + p_hdr->i_add_size;
assert( i_size >= 0 );
while( i_size > 0 ) while( i_size > 0 )
{ {
int i_skip = __MIN( i_size, INT_MAX ); int i_skip = __MIN( i_size, INT_MAX );
...@@ -444,7 +441,7 @@ static int SkipFile( stream_t *s,const rar_block_t *p_hdr ) ...@@ -444,7 +441,7 @@ static int SkipFile( stream_t *s,const rar_block_t *p_hdr )
int i_min_size = 7+21; int i_min_size = 7+21;
if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH ) if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH )
i_min_size += 8; i_min_size += 8;
if( p_hdr->i_size < i_min_size ) if( p_hdr->i_size < (unsigned)i_min_size )
return VLC_EGENERIC; return VLC_EGENERIC;
if( stream_Peek( s->p_source, &p_peek, i_min_size ) < i_min_size ) if( stream_Peek( s->p_source, &p_peek, i_min_size ) < i_min_size )
...@@ -481,7 +478,7 @@ static int SkipFile( stream_t *s,const rar_block_t *p_hdr ) ...@@ -481,7 +478,7 @@ static int SkipFile( stream_t *s,const rar_block_t *p_hdr )
} }
/* Ignore smaller files */ /* Ignore smaller files */
const int64_t i_file_size = ((int64_t)i_file_size_high << 32) | i_file_size_low; const uint64_t i_file_size = ((uint64_t)i_file_size_high << 32) | i_file_size_low;
if( p_sys->p_file && if( p_sys->p_file &&
p_sys->p_file->i_size < i_file_size ) p_sys->p_file->i_size < i_file_size )
{ {
......
...@@ -2186,7 +2186,7 @@ static bool Control( input_thread_t *p_input, ...@@ -2186,7 +2186,7 @@ static bool Control( input_thread_t *p_input,
else if( bookmark.i_byte_offset >= 0 && else if( bookmark.i_byte_offset >= 0 &&
p_input->p->input.p_stream ) p_input->p->input.p_stream )
{ {
const int64_t i_size = stream_Size( p_input->p->input.p_stream ); const uint64_t i_size = stream_Size( p_input->p->input.p_stream );
if( i_size > 0 && bookmark.i_byte_offset <= i_size ) if( i_size > 0 && bookmark.i_byte_offset <= i_size )
{ {
val.f_float = (double)bookmark.i_byte_offset / i_size; val.f_float = (double)bookmark.i_byte_offset / i_size;
......
This diff is collapsed.
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
#endif #endif
#include <limits.h>
#include "demux.h" #include "demux.h"
#include <libvlc.h> #include <libvlc.h>
...@@ -38,7 +39,7 @@ struct stream_sys_t ...@@ -38,7 +39,7 @@ struct stream_sys_t
block_fifo_t *p_fifo; block_fifo_t *p_fifo;
block_t *p_block; block_t *p_block;
int64_t i_pos; uint64_t i_pos;
/* Demuxer */ /* Demuxer */
char *psz_name; char *psz_name;
...@@ -223,13 +224,13 @@ static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_pee ...@@ -223,13 +224,13 @@ static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_pee
static int DStreamControl( stream_t *s, int i_query, va_list args ) static int DStreamControl( stream_t *s, int i_query, va_list args )
{ {
stream_sys_t *p_sys = s->p_sys; stream_sys_t *p_sys = s->p_sys;
int64_t *p_i64; uint64_t *p_i64;
bool *p_b; bool *p_b;
switch( i_query ) switch( i_query )
{ {
case STREAM_GET_SIZE: case STREAM_GET_SIZE:
p_i64 = (int64_t*) va_arg( args, int64_t * ); p_i64 = va_arg( args, uint64_t * );
*p_i64 = 0; *p_i64 = 0;
return VLC_SUCCESS; return VLC_SUCCESS;
...@@ -244,21 +245,22 @@ static int DStreamControl( stream_t *s, int i_query, va_list args ) ...@@ -244,21 +245,22 @@ static int DStreamControl( stream_t *s, int i_query, va_list args )
return VLC_SUCCESS; return VLC_SUCCESS;
case STREAM_GET_POSITION: case STREAM_GET_POSITION:
p_i64 = (int64_t*) va_arg( args, int64_t * ); p_i64 = va_arg( args, uint64_t * );
*p_i64 = p_sys->i_pos; *p_i64 = p_sys->i_pos;
return VLC_SUCCESS; return VLC_SUCCESS;
case STREAM_SET_POSITION: case STREAM_SET_POSITION:
{ {
int64_t i64 = (int64_t)va_arg( args, int64_t ); uint64_t i64 = va_arg( args, uint64_t );
int i_skip; if( i64 < p_sys->i_pos )
if( i64 < p_sys->i_pos ) return VLC_EGENERIC; return VLC_EGENERIC;
i_skip = i64 - p_sys->i_pos;
uint64_t i_skip = i64 - p_sys->i_pos;
while( i_skip > 0 ) while( i_skip > 0 )
{ {
int i_read = DStreamRead( s, NULL, (long)i_skip ); int i_read = DStreamRead( s, NULL, __MIN(i_skip, INT_MAX) );
if( i_read <= 0 ) return VLC_EGENERIC; if( i_read <= 0 )
return VLC_EGENERIC;
i_skip -= i_read; i_skip -= i_read;
} }
return VLC_SUCCESS; return VLC_SUCCESS;
......
...@@ -30,8 +30,8 @@ ...@@ -30,8 +30,8 @@
struct stream_sys_t struct stream_sys_t
{ {
bool i_preserve_memory; bool i_preserve_memory;
int64_t i_pos; /* Current reading offset */ uint64_t i_pos; /* Current reading offset */
int64_t i_size; uint64_t i_size;
uint8_t *p_buffer; uint8_t *p_buffer;
}; };
...@@ -51,7 +51,7 @@ static void Delete ( stream_t * ); ...@@ -51,7 +51,7 @@ static void Delete ( stream_t * );
* pointed to by p_buffer is freed on stream_Destroy * pointed to by p_buffer is freed on stream_Destroy
*/ */
stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer, stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
int64_t i_size, bool i_preserve_memory ) uint64_t i_size, bool i_preserve_memory )
{ {
stream_t *s = stream_CommonNew( p_this ); stream_t *s = stream_CommonNew( p_this );
stream_sys_t *p_sys; stream_sys_t *p_sys;
...@@ -103,13 +103,13 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -103,13 +103,13 @@ static int Control( stream_t *s, int i_query, va_list args )
stream_sys_t *p_sys = s->p_sys; stream_sys_t *p_sys = s->p_sys;
bool *p_bool; bool *p_bool;
int64_t *pi_64, i_64; uint64_t *pi_64, i_64;
int i_int; int i_int;
switch( i_query ) switch( i_query )
{ {
case STREAM_GET_SIZE: case STREAM_GET_SIZE:
pi_64 = (int64_t*)va_arg( args, int64_t * ); pi_64 = va_arg( args, uint64_t * );
*pi_64 = p_sys->i_size; *pi_64 = p_sys->i_size;
break; break;
...@@ -124,13 +124,12 @@ static int Control( stream_t *s, int i_query, va_list args ) ...@@ -124,13 +124,12 @@ static int Control( stream_t *s, int i_query, va_list args )
break; break;
case STREAM_GET_POSITION: case STREAM_GET_POSITION:
pi_64 = (int64_t*)va_arg( args, int64_t * ); pi_64 = va_arg( args, uint64_t * );
*pi_64 = p_sys->i_pos; *pi_64 = p_sys->i_pos;
break; break;
case STREAM_SET_POSITION: case STREAM_SET_POSITION:
i_64 = (int64_t)va_arg( args, int64_t ); i_64 = va_arg( args, uint64_t );
i_64 = __MAX( i_64, 0 );
i_64 = __MIN( i_64, s->p_sys->i_size ); i_64 = __MIN( i_64, s->p_sys->i_size );
p_sys->i_pos = i_64; p_sys->i_pos = i_64;
break; break;
......
...@@ -561,7 +561,7 @@ error: ...@@ -561,7 +561,7 @@ error:
static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status ) static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_status )
{ {
stream_t *p_stream = stream_UrlNew( p_vlm, psz_url ); stream_t *p_stream = stream_UrlNew( p_vlm, psz_url );
int64_t i_size; uint64_t i_size;
char *psz_buffer; char *psz_buffer;
if( !p_stream ) if( !p_stream )
...@@ -580,6 +580,8 @@ static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_st ...@@ -580,6 +580,8 @@ static int ExecuteLoad( vlm_t *p_vlm, const char *psz_url, vlm_message_t **pp_st
} }
i_size = stream_Size( p_stream ); i_size = stream_Size( p_stream );
if( i_size > SIZE_MAX - 1 )
i_size = SIZE_MAX - 1;
psz_buffer = malloc( i_size + 1 ); psz_buffer = malloc( i_size + 1 );
if( !psz_buffer ) if( !psz_buffer )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment