Commit 6d16eeca authored by eroman's avatar eroman Committed by Commit bot

Avoid signed integer overflow when calculating filesizes for VMS's ftp listings.

BUG=677863

Review-Url: https://codereview.chromium.org/2611933003
Cr-Commit-Position: refs/heads/master@{#441471}
parent e9359b8c
......@@ -6,6 +6,7 @@
#include <vector>
#include "base/numerics/safe_math.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
......@@ -55,6 +56,24 @@ bool ParseVmsFilename(const base::string16& raw_filename,
return true;
}
// VMS's directory listing gives file size in blocks. The exact file size is
// unknown both because it is measured in blocks, but also because the block
// size is unknown (but assumed to be 512 bytes).
bool ApproximateFilesizeFromBlockCount(int64_t num_blocks, int64_t* out_size) {
if (num_blocks < 0)
return false;
const int kBlockSize = 512;
base::CheckedNumeric<int64_t> num_bytes = num_blocks;
num_bytes *= kBlockSize;
if (!num_bytes.IsValid())
return false; // Block count is too large.
*out_size = num_bytes.ValueOrDie();
return true;
}
bool ParseVmsFilesize(const base::string16& input, int64_t* size) {
if (base::ContainsOnlyChars(input, base::ASCIIToUTF16("*"))) {
// Response consisting of asterisks means unknown size.
......@@ -62,17 +81,9 @@ bool ParseVmsFilesize(const base::string16& input, int64_t* size) {
return true;
}
// VMS's directory listing gives us file size in blocks. We assume that
// the block size is 512 bytes. It doesn't give accurate file size, but is the
// best information we have.
const int kBlockSize = 512;
if (base::StringToInt64(input, size)) {
if (*size < 0)
return false;
*size *= kBlockSize;
return true;
}
int64_t num_blocks;
if (base::StringToInt64(input, &num_blocks))
return ApproximateFilesizeFromBlockCount(num_blocks, size);
std::vector<base::StringPiece16> parts =
base::SplitStringPiece(input, base::ASCIIToUTF16("/"),
......@@ -90,8 +101,7 @@ bool ParseVmsFilesize(const base::string16& input, int64_t* size) {
if (blocks_used < 0 || blocks_allocated < 0)
return false;
*size = blocks_used * kBlockSize;
return true;
return ApproximateFilesizeFromBlockCount(blocks_used, size);
}
bool LooksLikeVmsFileProtectionListingPart(const base::string16& input) {
......
......@@ -21,36 +21,33 @@ typedef FtpDirectoryListingParserTest FtpDirectoryListingParserVmsTest;
TEST_F(FtpDirectoryListingParserVmsTest, Good) {
const struct SingleLineTestData good_cases[] = {
{ "README.TXT;4 2 18-APR-2000 10:40:39.90",
FtpDirectoryListingEntry::FILE, "readme.txt", 1024,
2000, 4, 18, 10, 40 },
{ ".WELCOME;1 2 13-FEB-2002 23:32:40.47",
FtpDirectoryListingEntry::FILE, ".welcome", 1024,
2002, 2, 13, 23, 32 },
{ "FILE.;1 2 13-FEB-2002 23:32:40.47",
FtpDirectoryListingEntry::FILE, "file.", 1024,
2002, 2, 13, 23, 32 },
{ "EXAMPLE.TXT;1 1 4-NOV-2009 06:02 [JOHNDOE] (RWED,RWED,,)",
FtpDirectoryListingEntry::FILE, "example.txt", 512,
2009, 11, 4, 6, 2 },
{ "ANNOUNCE.TXT;2 1/16 12-MAR-2005 08:44:57 [SYSTEM] (RWED,RWED,RE,RE)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512,
2005, 3, 12, 8, 44 },
{ "TEST.DIR;1 1 4-MAR-1999 22:14:34 [UCX$NOBO,ANONYMOUS] (RWE,RWE,RWE,RWE)",
FtpDirectoryListingEntry::DIRECTORY, "test", -1,
1999, 3, 4, 22, 14 },
{ "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (,,,)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512,
2005, 3, 12, 8, 44 },
{ "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (R,RW,RWD,RE)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512,
2005, 3, 12, 8, 44 },
{ "ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (ED,RED,WD,WED)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512,
2005, 3, 12, 8, 44 },
{ "VMS721.ISO;2 ****** 6-MAY-2008 09:29 [ANONY,ANONYMOUS] (RE,RWED,RE,RE)",
FtpDirectoryListingEntry::FILE, "vms721.iso", -1,
2008, 5, 6, 9, 29 },
{"README.TXT;4 2 18-APR-2000 10:40:39.90",
FtpDirectoryListingEntry::FILE, "readme.txt", 1024, 2000, 4, 18, 10, 40},
{".WELCOME;1 2 13-FEB-2002 23:32:40.47",
FtpDirectoryListingEntry::FILE, ".welcome", 1024, 2002, 2, 13, 23, 32},
{"FILE.;1 2 13-FEB-2002 23:32:40.47", FtpDirectoryListingEntry::FILE,
"file.", 1024, 2002, 2, 13, 23, 32},
{"EXAMPLE.TXT;1 1 4-NOV-2009 06:02 [JOHNDOE] (RWED,RWED,,)",
FtpDirectoryListingEntry::FILE, "example.txt", 512, 2009, 11, 4, 6, 2},
{"ANNOUNCE.TXT;2 1/16 12-MAR-2005 08:44:57 [SYSTEM] (RWED,RWED,RE,RE)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512, 2005, 3, 12, 8, 44},
{"TEST.DIR;1 1 4-MAR-1999 22:14:34 [UCX$NOBO,ANONYMOUS] "
"(RWE,RWE,RWE,RWE)",
FtpDirectoryListingEntry::DIRECTORY, "test", -1, 1999, 3, 4, 22, 14},
{"ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (,,,)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512, 2005, 3, 12, 8, 44},
{"ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (R,RW,RWD,RE)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512, 2005, 3, 12, 8, 44},
{"ANNOUNCE.TXT;2 1 12-MAR-2005 08:44:57 [X] (ED,RED,WD,WED)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512, 2005, 3, 12, 8, 44},
{"VMS721.ISO;2 ****** 6-MAY-2008 09:29 [ANONY,ANONYMOUS] "
"(RE,RWED,RE,RE)",
FtpDirectoryListingEntry::FILE, "vms721.iso", -1, 2008, 5, 6, 9, 29},
// This has an unusually large allocated block size (INT64_MAX), but
// shouldn't matter as it is not used.
{"ANNOUNCE.TXT;2 1/9223372036854775807 12-MAR-2005 08:44:57 [SYSTEM] "
"(RWED,RWED,RE,RE)",
FtpDirectoryListingEntry::FILE, "announce.txt", 512, 2005, 3, 12, 8, 44},
};
for (size_t i = 0; i < arraysize(good_cases); i++) {
SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i,
......@@ -90,10 +87,8 @@ TEST_F(FtpDirectoryListingParserVmsTest, Bad) {
// Malformed date.
"README.TXT;1 2 APR-2000 10:40:39",
"README.TXT;1 2 -18-APR-2000 10:40:39",
"README.TXT;1 2 18-APR 10:40:39",
"README.TXT;1 2 18-APR-2000 10",
"README.TXT;1 2 18-APR-2000 10:40.25",
"README.TXT;1 2 -18-APR-2000 10:40:39", "README.TXT;1 2 18-APR 10:40:39",
"README.TXT;1 2 18-APR-2000 10", "README.TXT;1 2 18-APR-2000 10:40.25",
"README.TXT;1 2 18-APR-2000 10:40.25.25",
// Malformed security information.
......@@ -107,6 +102,18 @@ TEST_F(FtpDirectoryListingParserVmsTest, Bad) {
"X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,DEWR,RE,RE)",
"X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RWED,Q,RE)",
"X.TXT;2 1 12-MAR-2005 08:44:57 [X] (RWED,RRWWEEDD,RE,RE)",
// Block size (INT64_MAX) is too large -- will overflow when
// multiplying by 512 to calculate the file size in bytes.
"README.TXT;1 9223372036854775807 18-APR-2000 10:40:39.90",
"README.TXT;1 9223372036854775807/9223372036854775807 18-APR-2000 "
"10:40:39.90",
// Block size (larger than INT64_MAX) is too large -- will fail to
// parse to an int64_t
"README.TXT;1 19223372036854775807 18-APR-2000 10:40:39.90",
"README.TXT;1 19223372036854775807/19223372036854775807 18-APR-2000 "
"10:40:39.90",
};
for (size_t i = 0; i < arraysize(bad_cases); i++) {
SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, bad_cases[i]));
......
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