~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/readline.cc

Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.

This fixes the buffer overflow in https://bugs.launchpad.net/drizzle/+bug/373468

It also removes a handwritten snprintf in field/datetime.cc
However... this caused us to have to change Temporal to have a way to not
"convert" the int64_t value (so 20090101 becomes 20090101000000 etc) as it
has already been converted and we just want the Temporal type to do the
to_string conversion.

This still causes a failure in 'metadata' test due to size of timestamp type. I need feedback from Jay on when the usecond code comes into play to know the correct fix for this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* readline for batch mode */
17
17
 
18
 
#include "config.h"
19
 
#include "drizzled/internal/my_sys.h"
20
 
#include "client/linebuffer.h"
21
 
 
22
 
#include <vector>
 
18
#include <drizzled/global.h>
 
19
#include <mysys/my_sys.h>
 
20
#include <mystrings/m_string.h>
 
21
#include "client/my_readline.h"
23
22
 
24
23
using namespace std;
25
 
using namespace drizzled;
26
 
 
27
 
LineBuffer::LineBuffer(uint32_t my_max_size,FILE *my_file)
28
 
  :
29
 
    file(my_file),
30
 
    line(),
31
 
    max_size(my_max_size),
32
 
    eof(false)
33
 
{
34
 
  line.reserve(max_size);
35
 
}
36
 
 
37
 
void LineBuffer::addString(const string &str)
38
 
{
39
 
  buffer << str << endl;
40
 
}
41
 
 
42
 
char *LineBuffer::readline()
43
 
{
44
 
  uint32_t read_count;
45
 
 
46
 
  if (file && !eof)
47
 
  {
48
 
    if ((read_count= internal::my_read(fileno(file),
49
 
                             (unsigned char *) (&line[0]),
50
 
                             max_size-1,MYF(MY_WME))))
 
24
 
 
25
static bool init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,
 
26
                            uint32_t max_size);
 
27
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str);
 
28
static size_t fill_buffer(LINE_BUFFER *buffer);
 
29
static char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length);
 
30
 
 
31
 
 
32
LINE_BUFFER *batch_readline_init(uint32_t max_size,FILE *file)
 
33
{
 
34
  LINE_BUFFER *line_buff;
 
35
  if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
 
36
    return 0;
 
37
  memset(line_buff, 0, sizeof(*line_buff));
 
38
  if (init_line_buffer(line_buff,fileno(file),IO_SIZE,max_size))
 
39
  {
 
40
    free(line_buff);
 
41
    return 0;
 
42
  }
 
43
  return line_buff;
 
44
}
 
45
 
 
46
 
 
47
char *batch_readline(LINE_BUFFER *line_buff)
 
48
{
 
49
  char *pos;
 
50
  uint32_t out_length;
 
51
 
 
52
  if (!(pos=intern_read_line(line_buff,&out_length)))
 
53
    return 0;
 
54
  if (out_length && pos[out_length-1] == '\n')
 
55
    if (--out_length && pos[out_length-1] == '\r')  /* Remove '\n' */
 
56
      out_length--;                                 /* Remove '\r' */
 
57
  line_buff->read_length=out_length;
 
58
  pos[out_length]=0;
 
59
  return pos;
 
60
}
 
61
 
 
62
 
 
63
void batch_readline_end(LINE_BUFFER *line_buff)
 
64
{
 
65
  if (line_buff)
 
66
  {
 
67
    free(line_buff->buffer);
 
68
    free(line_buff);
 
69
  }
 
70
}
 
71
 
 
72
 
 
73
LINE_BUFFER *batch_readline_command(LINE_BUFFER *line_buff, char * str)
 
74
{
 
75
  if (!line_buff)
 
76
  {
 
77
    if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
 
78
      return 0;
 
79
    memset(line_buff, 0, sizeof(*line_buff));
 
80
  }
 
81
  if (init_line_buffer_from_string(line_buff,str))
 
82
  {
 
83
    free(line_buff);
 
84
    return 0;
 
85
  }
 
86
  return line_buff;
 
87
}
 
88
 
 
89
 
 
90
/*****************************************************************************
 
91
      Functions to handle buffered readings of lines from a stream
 
92
******************************************************************************/
 
93
 
 
94
static bool
 
95
init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,uint32_t max_buffer)
 
96
{
 
97
  buffer->file=file;
 
98
  buffer->bufread=size;
 
99
  buffer->max_size=max_buffer;
 
100
  if (!(buffer->buffer = (char*) malloc(buffer->bufread+1)))
 
101
    return 1;
 
102
  buffer->end_of_line=buffer->end=buffer->buffer;
 
103
  buffer->buffer[0]=0;                          /* For easy start test */
 
104
  return 0;
 
105
}
 
106
 
 
107
/*
 
108
  init_line_buffer_from_string can be called on the same buffer
 
109
  several times. the resulting buffer will contain a
 
110
  concatenation of all strings separated by spaces
 
111
*/
 
112
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str)
 
113
{
 
114
  uint32_t old_length=(uint32_t)(buffer->end - buffer->buffer);
 
115
  uint32_t length= (uint32_t) strlen(str);
 
116
  char * tmpptr= (char*)realloc(buffer->buffer, old_length+length+2);
 
117
  if (tmpptr == NULL)
 
118
    return 1;
 
119
  
 
120
  buffer->buffer= buffer->start_of_line= buffer->end_of_line= tmpptr;
 
121
  buffer->end= buffer->buffer + old_length;
 
122
  if (old_length)
 
123
    buffer->end[-1]=' ';
 
124
  memcpy(buffer->end, str, length);
 
125
  buffer->end[length]= '\n';
 
126
  buffer->end[length+1]= 0;
 
127
  buffer->end+= length+1;
 
128
  buffer->eof=1;
 
129
  buffer->max_size=1;
 
130
  return 0;
 
131
}
 
132
 
 
133
 
 
134
/*
 
135
  Fill the buffer retaining the last n bytes at the beginning of the
 
136
  newly filled buffer (for backward context).   Returns the number of new
 
137
  bytes read from disk.
 
138
*/
 
139
 
 
140
static size_t fill_buffer(LINE_BUFFER *buffer)
 
141
{
 
142
  size_t read_count;
 
143
  uint32_t bufbytes= (uint32_t) (buffer->end - buffer->start_of_line);
 
144
 
 
145
  if (buffer->eof)
 
146
    return 0;                                   /* Everything read */
 
147
 
 
148
  /* See if we need to grow the buffer. */
 
149
 
 
150
  for (;;)
 
151
  {
 
152
    uint32_t start_offset=(uint32_t) (buffer->start_of_line - buffer->buffer);
 
153
    read_count=(buffer->bufread - bufbytes)/IO_SIZE;
 
154
    if ((read_count*=IO_SIZE))
 
155
      break;
 
156
    buffer->bufread *= 2;
 
157
    if (!(buffer->buffer = (char*) realloc(buffer->buffer,
 
158
                                           buffer->bufread+1)))
 
159
      return (uint32_t) -1;
 
160
    buffer->start_of_line=buffer->buffer+start_offset;
 
161
    buffer->end=buffer->buffer+bufbytes;
 
162
  }
 
163
 
 
164
  /* Shift stuff down. */
 
165
  if (buffer->start_of_line != buffer->buffer)
 
166
  {
 
167
    memmove(buffer->buffer, buffer->start_of_line, (uint32_t) bufbytes);
 
168
    buffer->end=buffer->buffer+bufbytes;
 
169
  }
 
170
 
 
171
  /* Read in new stuff. */
 
172
  if ((read_count= my_read(buffer->file, (unsigned char*) buffer->end, read_count,
 
173
                           MYF(MY_WME))) == MY_FILE_ERROR)
 
174
    return (size_t) -1;
 
175
 
 
176
  /* Kludge to pretend every nonempty file ends with a newline. */
 
177
  if (!read_count && bufbytes && buffer->end[-1] != '\n')
 
178
  {
 
179
    buffer->eof = read_count = 1;
 
180
    *buffer->end = '\n';
 
181
  }
 
182
  buffer->end_of_line=(buffer->start_of_line=buffer->buffer)+bufbytes;
 
183
  buffer->end+=read_count;
 
184
  *buffer->end=0;                               /* Sentinel */
 
185
  return read_count;
 
186
}
 
187
 
 
188
 
 
189
 
 
190
char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length)
 
191
{
 
192
  char *pos;
 
193
  size_t length;
 
194
 
 
195
 
 
196
  buffer->start_of_line=buffer->end_of_line;
 
197
  for (;;)
 
198
  {
 
199
    pos=buffer->end_of_line;
 
200
    while (*pos != '\n' && *pos)
 
201
      pos++;
 
202
    if (pos == buffer->end)
51
203
    {
52
 
      line[read_count+1]= '\0';
53
 
      buffer << &line[0];
 
204
      if ((uint32_t) (pos - buffer->start_of_line) < buffer->max_size)
 
205
      {
 
206
        if (!(length=fill_buffer(buffer)) || length == (size_t) -1)
 
207
          return(0);
 
208
        continue;
 
209
      }
 
210
      pos--;                                    /* break line here */
54
211
    }
55
 
    else
56
 
      eof= true;
 
212
    buffer->end_of_line=pos+1;
 
213
    *out_length=(uint32_t) (pos + 1 - buffer->eof - buffer->start_of_line);
 
214
    return(buffer->start_of_line);
57
215
  }
58
 
 
59
 
  buffer.getline(&line[0],max_size);
60
 
 
61
 
  if (buffer.eof())
62
 
    return 0;
63
 
  else
64
 
    return &line[0];
65
216
}
66