liblok: check new methods via macros on nSize, not by de-referencing.

We can't check for NULL from beyond the end of a smaller structure.

Change-Id: Id3754bf747c402cf0d767eda5fd4b5ad6b5789e9
This commit is contained in:
Michael Meeks 2014-06-12 13:28:26 +01:00
parent 4ed5bacc86
commit 202dac6e20
3 changed files with 21 additions and 1 deletions

View File

@ -18,6 +18,13 @@ extern "C"
typedef struct _LibreOfficeKit LibreOfficeKit; typedef struct _LibreOfficeKit LibreOfficeKit;
typedef struct _LibreOfficeKitDocument LibreOfficeKitDocument; typedef struct _LibreOfficeKitDocument LibreOfficeKitDocument;
// Do we have an extended member in this struct ?
#define LIBREOFFICEKIT_HAS_MEMBER(strct,member,nSize) \
((((int)((unsigned char *)&((strct *) 0)->member) + \
(int)sizeof ((strct *) 0)->member)) <= (nSize))
#define LIBREOFFICEKIT_HAS(pKit,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKit,member,(pKit)->nSize)
struct _LibreOfficeKit struct _LibreOfficeKit
{ {
int nSize; int nSize;
@ -28,6 +35,8 @@ struct _LibreOfficeKit
char* (*getError) (LibreOfficeKit *pThis); char* (*getError) (LibreOfficeKit *pThis);
}; };
#define LIBREOFFICEKIT_DOCUMENT_HAS(pDoc,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKitDocument,member,(pDoc)->nSize)
struct _LibreOfficeKitDocument struct _LibreOfficeKitDocument
{ {
int nSize; int nSize;

View File

@ -45,11 +45,12 @@ public:
inline bool saveAsWithOptions(const char* pUrl, const char* pFormat = NULL, const char* pFilterOptions = NULL) inline bool saveAsWithOptions(const char* pUrl, const char* pFormat = NULL, const char* pFilterOptions = NULL)
{ {
// available since LibreOffice 4.3 // available since LibreOffice 4.3
if (!mpDoc->saveAsWithOptions) if (!LIBREOFFICEKIT_DOCUMENT_HAS(mpDoc, saveAsWithOptions))
return false; return false;
return mpDoc->saveAsWithOptions(mpDoc, pUrl, pFormat, pFilterOptions); return mpDoc->saveAsWithOptions(mpDoc, pUrl, pFormat, pFilterOptions);
} }
inline LibreOfficeKitDocument *get() { return mpDoc; }
}; };
class Office class Office

View File

@ -75,6 +75,16 @@ int main (int argc, char **argv)
return -1; return -1;
} }
if (!LIBREOFFICEKIT_DOCUMENT_HAS(pDocument->get(), saveAsWithOptions))
{
fprintf( stderr, "using obsolete LibreOffice %d + %d vs. %d\n",
(int)((unsigned char *)&((LibreOfficeKitDocument *) 0)->saveAsWithOptions),
(int)sizeof ((LibreOfficeKitDocument *) 0)->saveAsWithOptions,
pDocument->get()->nSize );
return -1;
}
end = getTimeMS(); end = getTimeMS();
fprintf( stderr, "load time: %ld ms\n", (end-start) ); fprintf( stderr, "load time: %ld ms\n", (end-start) );
start = end; start = end;