Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: net/ftp/ftp_directory_listing_parser_vms.cc

Issue 2611933003: Avoid signed integer overflow when calculating filesizes for VMS's ftp listings. (Closed)
Patch Set: moar test Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/ftp/ftp_directory_listing_parser_vms_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/ftp/ftp_directory_listing_parser_vms.h" 5 #include "net/ftp/ftp_directory_listing_parser_vms.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/numerics/safe_math.h"
9 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h" 14 #include "base/time/time.h"
14 #include "net/ftp/ftp_directory_listing_parser.h" 15 #include "net/ftp/ftp_directory_listing_parser.h"
15 #include "net/ftp/ftp_util.h" 16 #include "net/ftp/ftp_util.h"
16 17
17 namespace net { 18 namespace net {
18 19
(...skipping 29 matching lines...) Expand all
48 if (base::EqualsASCII(filename_parts[1], "DIR")) { 49 if (base::EqualsASCII(filename_parts[1], "DIR")) {
49 *parsed_filename = base::ToLowerASCII(filename_parts[0]); 50 *parsed_filename = base::ToLowerASCII(filename_parts[0]);
50 *type = FtpDirectoryListingEntry::DIRECTORY; 51 *type = FtpDirectoryListingEntry::DIRECTORY;
51 } else { 52 } else {
52 *parsed_filename = base::ToLowerASCII(listing_parts[0]); 53 *parsed_filename = base::ToLowerASCII(listing_parts[0]);
53 *type = FtpDirectoryListingEntry::FILE; 54 *type = FtpDirectoryListingEntry::FILE;
54 } 55 }
55 return true; 56 return true;
56 } 57 }
57 58
59 // VMS's directory listing gives file size in blocks. The exact file size is
60 // unknown both because it is measured in blocks, but also because the block
61 // size is unknown (but assumed to be 512 bytes).
62 bool ApproximateFilesizeFromBlockCount(int64_t num_blocks, int64_t* out_size) {
63 if (num_blocks < 0)
64 return false;
65
66 const int kBlockSize = 512;
67 base::CheckedNumeric<int64_t> num_bytes = num_blocks;
68 num_bytes *= kBlockSize;
69
70 if (!num_bytes.IsValid())
71 return false; // Block count is too large.
72
73 *out_size = num_bytes.ValueOrDie();
74 return true;
75 }
76
58 bool ParseVmsFilesize(const base::string16& input, int64_t* size) { 77 bool ParseVmsFilesize(const base::string16& input, int64_t* size) {
59 if (base::ContainsOnlyChars(input, base::ASCIIToUTF16("*"))) { 78 if (base::ContainsOnlyChars(input, base::ASCIIToUTF16("*"))) {
60 // Response consisting of asterisks means unknown size. 79 // Response consisting of asterisks means unknown size.
61 *size = -1; 80 *size = -1;
62 return true; 81 return true;
63 } 82 }
64 83
65 // VMS's directory listing gives us file size in blocks. We assume that 84 int64_t num_blocks;
66 // the block size is 512 bytes. It doesn't give accurate file size, but is the 85 if (base::StringToInt64(input, &num_blocks))
67 // best information we have. 86 return ApproximateFilesizeFromBlockCount(num_blocks, size);
68 const int kBlockSize = 512;
69
70 if (base::StringToInt64(input, size)) {
71 if (*size < 0)
72 return false;
73 *size *= kBlockSize;
74 return true;
75 }
76 87
77 std::vector<base::StringPiece16> parts = 88 std::vector<base::StringPiece16> parts =
78 base::SplitStringPiece(input, base::ASCIIToUTF16("/"), 89 base::SplitStringPiece(input, base::ASCIIToUTF16("/"),
79 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 90 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
80 if (parts.size() != 2) 91 if (parts.size() != 2)
81 return false; 92 return false;
82 93
83 int64_t blocks_used, blocks_allocated; 94 int64_t blocks_used, blocks_allocated;
84 if (!base::StringToInt64(parts[0], &blocks_used)) 95 if (!base::StringToInt64(parts[0], &blocks_used))
85 return false; 96 return false;
86 if (!base::StringToInt64(parts[1], &blocks_allocated)) 97 if (!base::StringToInt64(parts[1], &blocks_allocated))
87 return false; 98 return false;
88 if (blocks_used > blocks_allocated) 99 if (blocks_used > blocks_allocated)
89 return false; 100 return false;
90 if (blocks_used < 0 || blocks_allocated < 0) 101 if (blocks_used < 0 || blocks_allocated < 0)
91 return false; 102 return false;
92 103
93 *size = blocks_used * kBlockSize; 104 return ApproximateFilesizeFromBlockCount(blocks_used, size);
94 return true;
95 } 105 }
96 106
97 bool LooksLikeVmsFileProtectionListingPart(const base::string16& input) { 107 bool LooksLikeVmsFileProtectionListingPart(const base::string16& input) {
98 if (input.length() > 4) 108 if (input.length() > 4)
99 return false; 109 return false;
100 110
101 // On VMS there are four different permission bits: Read, Write, Execute, 111 // On VMS there are four different permission bits: Read, Write, Execute,
102 // and Delete. They appear in that order in the permission listing. 112 // and Delete. They appear in that order in the permission listing.
103 std::string pattern("RWED"); 113 std::string pattern("RWED");
104 base::string16 match(input); 114 base::string16 match(input);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 entries->push_back(entry); 304 entries->push_back(entry);
295 } 305 }
296 306
297 // The only place where we return true is after receiving the "Total" line, 307 // The only place where we return true is after receiving the "Total" line,
298 // that should be present in every VMS listing. Alternatively, if the listing 308 // that should be present in every VMS listing. Alternatively, if the listing
299 // contains error messages, it's OK not to have the "Total" line. 309 // contains error messages, it's OK not to have the "Total" line.
300 return seen_error; 310 return seen_error;
301 } 311 }
302 312
303 } // namespace net 313 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/ftp/ftp_directory_listing_parser_vms_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698