comparison playtreeparser.c @ 28182:26ab2d01157f

Add asserts to detect when assumptions for play_tree_parser_get_line fail (mostly due to parsers using it incorrectly).
author reimar
date Thu, 01 Jan 2009 09:35:25 +0000
parents fba022abe464
children b197740bf718
comparison
equal deleted inserted replaced
28181:a02653498c15 28182:26ab2d01157f
4 4
5 #include "config.h" 5 #include "config.h"
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h> 8 #include <string.h>
9 #ifdef MP_DEBUG
10 #include <assert.h> 9 #include <assert.h>
11 #endif
12 #include <errno.h> 10 #include <errno.h>
13 #include <sys/types.h> 11 #include <sys/types.h>
14 #include <sys/stat.h> 12 #include <sys/stat.h>
15 #include <fcntl.h> 13 #include <fcntl.h>
16 #include <unistd.h> 14 #include <unistd.h>
59 } 57 }
60 58
61 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0')) 59 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
62 return NULL; 60 return NULL;
63 61
62 assert(p->buffer_end < p->buffer_size);
63 assert(!p->buffer[p->buffer_end]);
64 while(1) { 64 while(1) {
65 65
66 if(resize) { 66 if(resize) {
67 r = p->iter - p->buffer; 67 r = p->iter - p->buffer;
68 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP); 68 p->buffer = (char*)realloc(p->buffer,p->buffer_size+BUF_STEP);
73 73
74 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) { 74 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
75 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1); 75 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
76 if(r > 0) { 76 if(r > 0) {
77 p->buffer_end += r; 77 p->buffer_end += r;
78 assert(p->buffer_end < p->buffer_size);
78 p->buffer[p->buffer_end] = '\0'; 79 p->buffer[p->buffer_end] = '\0';
79 while(strlen(p->buffer + p->buffer_end - r) != r) 80 while(strlen(p->buffer + p->buffer_end - r) != r)
80 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n'; 81 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
81 } 82 }
83 assert(!p->buffer[p->buffer_end]);
82 } 84 }
83 85
84 end = strchr(p->iter,'\n'); 86 end = strchr(p->iter,'\n');
85 if(!end) { 87 if(!end) {
86 if(p->stream->eof) { 88 if(p->stream->eof) {