~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/command_reader.cc

MergeĀ fromĀ lp:~jaypipes/drizzle/serial_event_log

Show diffs side-by-side

added added

removed removed

Lines of Context:
204
204
 
205
205
  char *buffer= NULL;
206
206
  char *temp_buffer;
207
 
  uint64_t previous_length= 0;
 
207
  ssize_t previous_length= 0;
 
208
  ssize_t read_bytes= 0;
 
209
  ssize_t length= 0;
208
210
 
209
 
  while (1)
 
211
  /* Read in the length of the command */
 
212
  while ((read_bytes= read(file, &length, sizeof(ssize_t))) != 0)
210
213
  {
211
 
    uint64_t length;
212
 
 
213
 
    /* Read the size */
214
 
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
215
 
      break;
216
 
 
217
 
    if (length > SIZE_MAX)
 
214
    if (read_bytes == -1)
 
215
    {
 
216
      cerr << "Failed to read initial length." << endl;
 
217
      exit(1);
 
218
    }
 
219
 
 
220
    if ((size_t) length > SIZE_MAX)
218
221
    {
219
222
      cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
220
223
      exit(1);
222
225
 
223
226
    /* If we've already allocated a buffer bigger than what's needed, just zero out the buffer... */
224
227
    if (length > previous_length)
 
228
    {
225
229
      temp_buffer= (char *) realloc(buffer, (size_t) length);
226
 
    else
227
 
      memset(temp_buffer, 0, sizeof(temp_buffer));
228
230
 
229
 
    if (temp_buffer == NULL)
230
 
    {
231
 
      cerr << "Memory allocation failure trying to allocate " << length << " bytes."  << endl;
232
 
      exit(1);
 
231
      if (temp_buffer == NULL)
 
232
      {
 
233
        cerr << "Memory allocation failure trying to allocate " << length << " bytes."  << endl;
 
234
        exit(1);
 
235
      }
233
236
    }
234
237
    
235
238
    buffer= temp_buffer;
236
 
    size_t read_bytes= 0;
237
239
 
238
 
    /* Read the transaction */
239
 
    if ((read_bytes= read(file, buffer, (size_t)length)) != (size_t)length)
 
240
    /* Read the Command */
 
241
    read_bytes= read(file, buffer, (size_t) length);
 
242
    if ((read_bytes != (ssize_t) length))
240
243
    {
241
244
      cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
242
245
      exit(1);
243
246
    }
244
 
    command.ParseFromArray(buffer, (int) length);
 
247
 
 
248
    if (! command.ParseFromArray(buffer, (int) length))
 
249
    {
 
250
      cerr << "Unable to parse command. Got error: " << command.InitializationErrorString() << endl;
 
251
      cerr << "BUFFER: " << buffer << endl;
 
252
      exit(1);
 
253
    }
245
254
 
246
255
    /* Print the command */
247
256
    printCommand(command);