summaryrefslogtreecommitdiffstats
path: root/stream
diff options
context:
space:
mode:
authorreimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2>2010-02-28 12:54:12 +0000
committerreimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2>2010-02-28 12:54:12 +0000
commitd8c02c2dd229b2fda576e0aa09cee4e0a6090b7b (patch)
tree5d74617022d336fbc4115d269934d894911b7b60 /stream
parent2c8d69f1855122e8ddece9e31b640db937d0ff9d (diff)
downloadmpv-d8c02c2dd229b2fda576e0aa09cee4e0a6090b7b.tar.bz2
mpv-d8c02c2dd229b2fda576e0aa09cee4e0a6090b7b.tar.xz
Simplify handling of 0-termination in stream_read_line
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@30795 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'stream')
-rw-r--r--stream/stream.h8
1 files changed, 5 insertions, 3 deletions
diff --git a/stream/stream.h b/stream/stream.h
index f1a3667343..579468114a 100644
--- a/stream/stream.h
+++ b/stream/stream.h
@@ -268,6 +268,8 @@ inline static int stream_read(stream_t *s,char* mem,int total){
inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max) {
int len;
unsigned char* end,*ptr = mem;
+ if (max < 1) return NULL;
+ max--; // reserve one for 0-termination
do {
len = s->buf_len-s->buf_pos;
// try to fill the buffer
@@ -276,8 +278,8 @@ inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, in
(len = s->buf_len-s->buf_pos) <= 0)) break;
end = (unsigned char*) memchr((void*)(s->buffer+s->buf_pos),'\n',len);
if(end) len = end - (s->buffer+s->buf_pos) + 1;
- if(len > 0 && max > 1) {
- int l = len > max-1 ? max-1 : len;
+ if(len > 0 && max > 0) {
+ int l = len > max ? max : len;
memcpy(ptr,s->buffer+s->buf_pos,l);
max -= l;
ptr += l;
@@ -285,7 +287,7 @@ inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, in
s->buf_pos += len;
} while(!end);
if(s->eof && ptr == mem) return NULL;
- if(max > 0) ptr[0] = 0;
+ ptr[0] = 0;
return mem;
}