~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

Change temporal to_string routines to use snprintf instead of sprintf.
Also change length parameter to be length of string to write to, and
add return value of length written (or what would have - i.e. snprintf
result).

This doesn't fix https://bugs.launchpad.net/drizzle/+bug/373468 but
it means we hit an assert() on being able to put the temporal type
into a string instead of overwriting the buffer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1184
1184
    return is_valid();
1185
1185
}
1186
1186
 
1187
 
void Time::to_string(char *to, size_t *to_len) const
1188
 
{
1189
 
  *to_len= sprintf(to
1190
 
                 , "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
1191
 
                 , _hours
1192
 
                 , _minutes
1193
 
                 , _seconds);
1194
 
}
1195
 
 
1196
 
void Date::to_string(char *to, size_t *to_len) const
1197
 
{
1198
 
  *to_len= sprintf(to
1199
 
                 , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1200
 
                 , _years
1201
 
                 , _months
1202
 
                 , _days);
1203
 
}
1204
 
 
1205
 
void DateTime::to_string(char *to, size_t *to_len) const
 
1187
int Time::to_string(char *to, size_t to_len) const
 
1188
{
 
1189
  return snprintf(to, to_len,
 
1190
                  "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
 
1191
                  _hours, _minutes, _seconds);
 
1192
}
 
1193
 
 
1194
int Date::to_string(char *to, size_t to_len) const
 
1195
{
 
1196
  return snprintf(to, to_len,
 
1197
                  "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32,
 
1198
                  _years, _months, _days);
 
1199
}
 
1200
 
 
1201
int DateTime::to_string(char *to, size_t to_len) const
1206
1202
{
1207
1203
  /* If the temporal has a microsecond component, use a slightly different output */
1208
1204
  if (_useconds == 0)
1209
1205
  {
1210
 
    *to_len= sprintf(to
1211
 
                  , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
1212
 
                  , _years
1213
 
                  , _months
1214
 
                  , _days
1215
 
                  , _hours
1216
 
                  , _minutes
1217
 
                  , _seconds);
 
1206
    return snprintf(to, to_len,
 
1207
                    "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
 
1208
                          " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
 
1209
                    _years, _months, _days,
 
1210
                    _hours, _minutes, _seconds);
1218
1211
  }
1219
1212
  else
1220
1213
  {
1221
 
    *to_len= sprintf(to
1222
 
                  , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
1223
 
                  , _years
1224
 
                  , _months
1225
 
                  , _days
1226
 
                  , _hours
1227
 
                  , _minutes
1228
 
                  , _seconds
1229
 
                  , _useconds);
 
1214
    return snprintf(to, to_len,
 
1215
                    "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
 
1216
                       " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
 
1217
                    _years, _months, _days,
 
1218
                    _hours, _minutes, _seconds, _useconds);
1230
1219
  }
1231
1220
}
1232
1221
 
1233
 
void MicroTimestamp::to_string(char *to, size_t *to_len) const
 
1222
int MicroTimestamp::to_string(char *to, size_t to_len) const
1234
1223
{
1235
 
  *to_len= sprintf(to
1236
 
                 , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
1237
 
                 , _years
1238
 
                 , _months
1239
 
                 , _days
1240
 
                 , _hours
1241
 
                 , _minutes
1242
 
                 , _seconds
1243
 
                 , _useconds);
 
1224
  return snprintf(to, to_len,
 
1225
                  "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
 
1226
                      " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
 
1227
                  _years, _months, _days,
 
1228
                  _hours, _minutes, _seconds, _useconds);
1244
1229
}
1245
1230
 
1246
1231
void Time::to_decimal(my_decimal *to) const