Bug 263801 Hang when importing invalid address book (csv file)

patch by mark@standard8.demon.co.uk r=bienvenu sr=mscott
This commit is contained in:
timeless%mozdev.org 2005-02-24 16:11:46 +00:00
Родитель 76d4b71659
Коммит 9319e69150
2 изменённых файлов: 23 добавлений и 39 удалений

Просмотреть файл

@ -161,6 +161,7 @@ nsresult nsTextAddress::ImportAddresses( PRBool *pAbort, const PRUnichar *pName,
nsresult nsTextAddress::ReadRecord( nsIFileSpec *pSrc, char *pLine, PRInt32 bufferSz, char delim, PRInt32 *pLineLen)
{
PRBool wasTruncated;
PRBool isEof;
char * pRead;
PRInt32 lineLen = 0;
nsresult rv;
@ -175,16 +176,24 @@ nsresult nsTextAddress::ReadRecord( nsIFileSpec *pSrc, char *pLine, PRInt32 buff
wasTruncated = PR_FALSE;
pRead = pLine;
pRead += lineLen;
rv = pSrc->ReadLine( &pRead, bufferSz - lineLen, &wasTruncated);
if (wasTruncated) {
pSrc->Eof(&isEof);
if (isEof)
// If we get an EOF here, then the line isn't complete
// so we must have an incorrect file.
rv = NS_ERROR_FAILURE;
else
{
rv = pSrc->ReadLine( &pRead, bufferSz - lineLen, &wasTruncated);
if (wasTruncated) {
pLine[bufferSz - 1] = 0;
IMPORT_LOG0( "Unable to read line from file, buffer too small\n");
rv = NS_ERROR_FAILURE;
}
else if (NS_SUCCEEDED( rv)) {
}
else if (NS_SUCCEEDED( rv)) {
lineLen = strlen( pLine);
}
}
} while (NS_SUCCEEDED( rv) && !IsLineComplete( pLine, lineLen, delim));
} while (NS_SUCCEEDED( rv) && !IsLineComplete( pLine, lineLen));
*pLineLen = lineLen;
return( rv);
@ -221,44 +230,19 @@ nsresult nsTextAddress::ReadRecordNumber( nsIFileSpec *pSrc, char *pLine, PRInt3
Find out if the given line appears to be a complete record or
if it needs more data because the line ends with a quoted value non-terminated
*/
PRBool nsTextAddress::IsLineComplete( const char *pLine, PRInt32 len, char delim)
PRBool nsTextAddress::IsLineComplete( const char *pLine, PRInt32 len)
{
char tab = 9;
if (delim == tab)
tab = 0;
PRBool quoted = PR_FALSE;
PRBool wasDelim = PR_FALSE;
while (len) {
// always skip white space?
while (len && ((*pLine == ' ') || (*pLine == tab))) {
pLine++;
len--;
}
if (len && wasDelim && (*pLine == '"')) {
quoted = PR_TRUE;
wasDelim = PR_FALSE;
pLine++;
len--;
}
else if (len && quoted && (*pLine == '"')) {
quoted = PR_FALSE;
pLine++;
len--;
}
else if (len) {
if (!quoted && (*pLine == delim))
wasDelim = PR_TRUE;
else
wasDelim = PR_FALSE;
pLine++;
len--;
}
if ((*pLine == '"')) {
quoted = !quoted;
}
pLine++;
len--;
}
if (quoted)
return( PR_FALSE);
return( PR_TRUE);
return !quoted;
}
PRInt32 nsTextAddress::CountFields( const char *pLine, PRInt32 maxLen, char delim)

Просмотреть файл

@ -75,7 +75,7 @@ public:
private:
nsresult ProcessLine( const char *pLine, PRInt32 len, nsString& errors);
static PRBool IsLineComplete( const char *pLine, PRInt32 len, char delim);
static PRBool IsLineComplete( const char *pLine, PRInt32 len);
static PRInt32 CountFields( const char *pLine, PRInt32 maxLen, char delim);
static void SanitizeSingleLine( nsCString& val);