Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport queued_ltr_backports] Use better approach for malloc size safety (logic ported from GDAL) #47129

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ QString qgsVsiPrefix( const QString &path );




const long GEOSRID;

const long GEOCRS_ID;
Expand Down
24 changes: 8 additions & 16 deletions src/core/qgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,22 @@ qlonglong qgsPermissiveToLongLong( QString string, bool &ok )

void *qgsMalloc( size_t size )
{
if ( size == 0 || long( size ) < 0 )
if ( size == 0 )
{
QgsDebugMsg( QStringLiteral( "Negative or zero size %1." ).arg( size ) );
QgsDebugMsg( QStringLiteral( "Zero size requested" ) );
return nullptr;
}
void *p = malloc( size );
if ( !p )
{
QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
}
return p;
}

void *qgsCalloc( size_t nmemb, size_t size )
{
if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
if ( ( size >> ( 8 * sizeof( size ) - 1 ) ) != 0 )
{
QgsDebugMsg( QStringLiteral( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
QgsDebugMsg( QStringLiteral( "qgsMalloc - bad size requested: %1" ).arg( size ) );
return nullptr;
}
void *p = qgsMalloc( nmemb * size );
if ( p )

void *p = malloc( size );
if ( !p )
{
memset( p, 0, nmemb * size );
QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
}
return p;
}
Expand Down
9 changes: 0 additions & 9 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1578,15 +1578,6 @@ CORE_EXPORT QString qgsVsiPrefix( const QString &path );
*/
void CORE_EXPORT *qgsMalloc( size_t size ) SIP_SKIP;

/**
* Allocates memory for an array of nmemb elements of size bytes each and returns
* a pointer to the allocated memory. Works like C calloc() but prints debug message
* by QgsLogger if allocation fails.
* \param nmemb number of elements
* \param size size of element in bytes
*/
void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size ) SIP_SKIP;

/**
* Frees the memory space pointed to by ptr. Works like C free().
* \param ptr pointer to memory space
Expand Down