Kaydet (Commit) 113536e9 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Avoid -Werror=format-{overflow,truncation}=

...as emitted by at least GCC 8.2 with --enable-optimized, by making the buffers
large enough for the (hypothetical) largest values of the various date/time
components

Change-Id: I82e9b08fa099546b2d6f29c702e1440df9e6c6e0
Reviewed-on: https://gerrit.libreoffice.org/66618
Tested-by: Jenkins
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 5e1e9120
...@@ -1822,16 +1822,17 @@ bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, const OValueRefRow& pOrgRo ...@@ -1822,16 +1822,17 @@ bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, const OValueRefRow& pOrgRo
aDate = ::dbtools::DBTypeConversion::toDate(thisColVal.getDouble()); aDate = ::dbtools::DBTypeConversion::toDate(thisColVal.getDouble());
else else
aDate = thisColVal; aDate = thisColVal;
char s[9]; char s[sizeof("-327686553565535")];
// reserve enough space for hypothetical max length
snprintf(s, snprintf(s,
sizeof(s), sizeof(s),
"%04d%02d%02d", "%04d%02d%02d",
static_cast<int>(aDate.Year), static_cast<sal_Int32>(aDate.Year),
static_cast<int>(aDate.Month), static_cast<sal_uInt32>(aDate.Month),
static_cast<int>(aDate.Day)); static_cast<sal_uInt32>(aDate.Day));
// Exactly 8 bytes to copy: // Exactly 8 bytes to copy (even if s could hypothetically be longer):
strncpy(pData,s,sizeof s - 1); memcpy(pData,s,8);
} break; } break;
case DataType::INTEGER: case DataType::INTEGER:
{ {
......
...@@ -98,11 +98,12 @@ namespace comphelper { namespace log { namespace convert ...@@ -98,11 +98,12 @@ namespace comphelper { namespace log { namespace convert
OUString convertLogArgToString( const DateTime& _rDateTime ) OUString convertLogArgToString( const DateTime& _rDateTime )
{ {
char buffer[ 30 ]; char buffer[ sizeof("-32768-65535-65535 65535:65535:65535.4294967295") ];
// reserve enough space for hypothetical max length
const size_t buffer_size = sizeof( buffer ); const size_t buffer_size = sizeof( buffer );
snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%09i", snprintf( buffer, buffer_size, "%04" SAL_PRIdINT32 "-%02" SAL_PRIuUINT32 "-%02" SAL_PRIuUINT32 " %02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ".%09" SAL_PRIuUINT32,
static_cast<int>(_rDateTime.Year), static_cast<int>(_rDateTime.Month), static_cast<int>(_rDateTime.Day), static_cast<sal_Int32>(_rDateTime.Year), static_cast<sal_uInt32>(_rDateTime.Month), static_cast<sal_uInt32>(_rDateTime.Day),
static_cast<int>(_rDateTime.Hours), static_cast<int>(_rDateTime.Minutes), static_cast<int>(_rDateTime.Seconds), static_cast<int>(_rDateTime.NanoSeconds) ); static_cast<sal_uInt32>(_rDateTime.Hours), static_cast<sal_uInt32>(_rDateTime.Minutes), static_cast<sal_uInt32>(_rDateTime.Seconds), _rDateTime.NanoSeconds );
return OUString::createFromAscii( buffer ); return OUString::createFromAscii( buffer );
} }
......
...@@ -79,7 +79,8 @@ namespace logging ...@@ -79,7 +79,8 @@ namespace logging
OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord )
{ {
char buffer[ 30 ]; char buffer[ sizeof("-32768-65535-65535 65535:65535:65535.4294967295") ];
// reserve enough space for hypothetical max length
const int buffer_size = sizeof( buffer ); const int buffer_size = sizeof( buffer );
int used = snprintf( buffer, buffer_size, "%10i", static_cast<int>(_rRecord.SequenceNumber) ); int used = snprintf( buffer, buffer_size, "%10i", static_cast<int>(_rRecord.SequenceNumber) );
if ( used >= buffer_size || used < 0 ) if ( used >= buffer_size || used < 0 )
...@@ -94,9 +95,9 @@ namespace logging ...@@ -94,9 +95,9 @@ namespace logging
aLogEntry.appendAscii( buffer ); aLogEntry.appendAscii( buffer );
aLogEntry.append( " " ); aLogEntry.append( " " );
snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%09i", snprintf( buffer, buffer_size, "%04" SAL_PRIdINT32 "-%02" SAL_PRIuUINT32 "-%02" SAL_PRIuUINT32 " %02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ".%09" SAL_PRIuUINT32,
static_cast<int>(_rRecord.LogTime.Year), static_cast<int>(_rRecord.LogTime.Month), static_cast<int>(_rRecord.LogTime.Day), static_cast<sal_Int32>(_rRecord.LogTime.Year), static_cast<sal_uInt32>(_rRecord.LogTime.Month), static_cast<sal_uInt32>(_rRecord.LogTime.Day),
static_cast<int>(_rRecord.LogTime.Hours), static_cast<int>(_rRecord.LogTime.Minutes), static_cast<int>(_rRecord.LogTime.Seconds), static_cast<int>(_rRecord.LogTime.NanoSeconds) ); static_cast<sal_uInt32>(_rRecord.LogTime.Hours), static_cast<sal_uInt32>(_rRecord.LogTime.Minutes), static_cast<sal_uInt32>(_rRecord.LogTime.Seconds), _rRecord.LogTime.NanoSeconds );
aLogEntry.appendAscii( buffer ); aLogEntry.appendAscii( buffer );
aLogEntry.append( " " ); aLogEntry.append( " " );
......
...@@ -1001,9 +1001,10 @@ bool PowerPointExport::WriteComments(sal_uInt32 nPageNum) ...@@ -1001,9 +1001,10 @@ bool PowerPointExport::WriteComments(sal_uInt32 nPageNum)
Reference< XText > xText(xAnnotation->getTextRange()); Reference< XText > xText(xAnnotation->getTextRange());
sal_Int32 nLastIndex; sal_Int32 nLastIndex;
sal_Int32 nId = GetAuthorIdAndLastIndex(xAnnotation->getAuthor(), nLastIndex); sal_Int32 nId = GetAuthorIdAndLastIndex(xAnnotation->getAuthor(), nLastIndex);
char cDateTime[32]; char cDateTime[sizeof("-32768-65535-65535T65535:65535:65535.4294967295")];
// reserve enough space for hypothetical max length
snprintf(cDateTime, 31, "%02d-%02d-%02dT%02d:%02d:%02d.%09" SAL_PRIuUINT32, aDateTime.Year, aDateTime.Month, aDateTime.Day, aDateTime.Hours, aDateTime.Minutes, aDateTime.Seconds, aDateTime.NanoSeconds); snprintf(cDateTime, sizeof cDateTime, "%02" SAL_PRIdINT32 "-%02" SAL_PRIuUINT32 "-%02" SAL_PRIuUINT32 "T%02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32 ".%09" SAL_PRIuUINT32, sal_Int32(aDateTime.Year), sal_uInt32(aDateTime.Month), sal_uInt32(aDateTime.Day), sal_uInt32(aDateTime.Hours), sal_uInt32(aDateTime.Minutes), sal_uInt32(aDateTime.Seconds), aDateTime.NanoSeconds);
pFS->startElementNS(XML_p, XML_cm, pFS->startElementNS(XML_p, XML_cm,
XML_authorId, I32S(nId), XML_authorId, I32S(nId),
......
...@@ -200,8 +200,9 @@ OUString LockFileCommon::GetCurrentLocalTime() ...@@ -200,8 +200,9 @@ OUString LockFileCommon::GetCurrentLocalTime()
oslDateTime aDateTime; oslDateTime aDateTime;
if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) ) if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
{ {
char pDateTime[20]; char pDateTime[sizeof("65535.65535.-32768 65535:65535")];
sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes ); // reserve enough space for hypothetical max length
sprintf( pDateTime, "%02" SAL_PRIuUINT32 ".%02" SAL_PRIuUINT32 ".%4" SAL_PRIdINT32 " %02" SAL_PRIuUINT32 ":%02" SAL_PRIuUINT32, sal_uInt32(aDateTime.Day), sal_uInt32(aDateTime.Month), sal_Int32(aDateTime.Year), sal_uInt32(aDateTime.Hours), sal_uInt32(aDateTime.Minutes) );
aTime = OUString::createFromAscii( pDateTime ); aTime = OUString::createFromAscii( pDateTime );
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment