The warning correctly comes from the inlining of the strsafe functions, specifically StringCchCopy().
The compiler, without any more spec, inlines a local definition for specific safe string functions. In your case you use StringCchCopy() that is defined, in strsafe.h, as:
#ifndef STRSAFE_NO_CCH_FUNCTIONS
#ifdef STRSAFE_INLINE
STRSAFEAPI StringCchCopyA(char *pszDest, size_t cchDest, const char *pszSrc)
{
if (cchDest > STRSAFE_MAX_CCH)
return STRSAFE_E_INVALID_PARAMETER;
else
return StringCopyWorkerA(pszDest, cchDest, pszSrc);
}
...
#ifdef STRSAFE_INLINE
STRSAFEAPI StringCopyWorkerA(char *pszDest, size_t cchDest, const char *pszSrc)
{
HRESULT hr = S_OK;
if (cchDest == 0)
{
hr = STRSAFE_E_INVALID_PARAMETER;
}
else
{
while (cchDest && *pszSrc != '\0')
{
*pszDest++ = *pszSrc++;
cchDest--;
}
if (cchDest == 0)
{
pszDest--;
hr = STRSAFE_E_INSUFFICIENT_BUFFER;
}
*pszDest= '\0';
}
return hr;
}
...
In your code you use always constants for max string length when calling StringCchCopy():
//C:\Users\Grincheux\Documents\Visual Studio 2015\Projects\Euphory\Sqlite.c(218): warning #2154: Unreachable code.
LPSTR SQLite_GetFileName(int __iItem,LPSTR __lpszFileName)
{
StringCchCopy(__lpszFileName,MAX_PATH,(LPSTR) sqlite3_column_text(_hStatement,0)) ;
...
//C:\Users\Grincheux\Documents\Visual Studio 2015\Projects\Euphory\Sqlite.c(645): warning #2154: Unreachable code.
int SQLite_Sort(HWND __hWnd,int _iId)
{
if(szCurrentFile[0] == '\0') StringCchCopy(szCurrentFile,MAX_PATH,_lpszImg) ;
...
StringCchCopy(szCurrentDialogFile,MAX_PATH,_lpszImg) ;
...
if(_szFirstFile[0] == '\0') StringCchCopy(szCurrentFile,MAX_PATH,_lpszImg) ;
...
StringCchCopy(szCurrentFile,MAX_PATH,_szFirstFile) ;
...
//C:\Users\Grincheux\Documents\Visual Studio 2015\Projects\Euphory\Sqlite.c(768): warning #2154: Unreachable code.
void SQLiteDB_BackupToCsv(HWND __hWnd)
{
...
StringCchCopy(_szTmp1,sizeof(_szTmp1),"\"Item\";\"Image\";\"lParam\";\"FileSize\";\"ImgSize\";\"ImgWidth\";\"ImgHeight\";\"ImgRatio\";\"MedianRed\";\"MedianGreen\";\"MedianBlue\";\"NumRed\";\"NumGreen\";\"NumBlue\";\"MaxRed\";\"MaxGreen\";\"MaxBlue\";\"AnalyzedColors\";\"NumberOfUniqueColors\";\"FileMD5\";\"ImgMD5\";\"CurrentFile\"\n") ;
...
StringCchCopy(_szTmp1,sizeof(_szTmp1),"An error occured during backup operation") ;
...
For this reason when inlining the function StringCopyWorkerA() happen that the first check is constant so the conditional code is never used:
STRSAFEAPI StringCopyWorkerA(char *pszDest, size_t cchDest, const char *pszSrc)
{
HRESULT hr = S_OK;
if (cchDest == 0) //if cchDest is a constant and always !=0
{
hr = STRSAFE_E_INVALID_PARAMETER; //This code will never execute, so it is **unreacheble**!!!!
}
As a check replace the main function in the project provided by Timo with the following:
LPSTR SQLite_GetFileName(int __iItem,LPSTR __lpszFileName)
{
sqlite3_stmt *_hStatement ;
LPSTR _lpszErrorMessage ;
char _szTmp[1024] ;
volatile int max = MAX_PATH;
if(!__lpszFileName)
return (NULL) ;
*__lpszFileName = '\0' ;
StringCbPrintf(_szTmp,sizeof(_szTmp),szSQL_GetFileName,__iItem) ;
if(sqlite3_prepare_v2(hSQLite,(const char *) _szTmp,-1,&_hStatement,(const char **) &_lpszErrorMessage) != SQLITE_OK)
return (NULL) ;
while(sqlite3_step(_hStatement) == SQLITE_ROW)
//StringCchCopy(__lpszFileName,MAX_PATH,(LPSTR) sqlite3_column_text(_hStatement,0)) ;
StringCchCopy(__lpszFileName,max,(LPSTR) sqlite3_column_text(_hStatement,0)) ;
sqlite3_reset(_hStatement) ;
sqlite3_finalize(_hStatement) ;
return (__lpszFileName) ;
}
Note the volatile variable max that tells the compiler to not assume that the value will be constant. In this case the condition if (cchDest == 0) cannot be resolved at compile time, the conditional code must be generated and the warning is not issued.