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

Unified Diff: runtime/vm/port.cc

Issue 217693002: - Make ports be less predictable. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/port.h ('k') | runtime/vm/random.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/port.cc
===================================================================
--- runtime/vm/port.cc (revision 34525)
+++ runtime/vm/port.cc (working copy)
@@ -20,7 +20,7 @@
intptr_t PortMap::capacity_ = 0;
intptr_t PortMap::used_ = 0;
intptr_t PortMap::deleted_ = 0;
-Dart_Port PortMap::next_port_ = 7111;
+Random* PortMap::prng_ = NULL;
intptr_t PortMap::FindPort(Dart_Port port) {
@@ -63,15 +63,17 @@
Dart_Port PortMap::AllocatePort() {
- Dart_Port result = next_port_;
+ const Dart_Port kMASK = 0x3fffffff;
+ Dart_Port result = prng_->NextUInt32() & kMASK;
- do {
- // TODO(iposva): Use an approved hashing function to have less predictable
- // port ids, or make them not accessible from Dart code or both.
- next_port_++;
- } while (FindPort(next_port_) >= 0);
+ // Keep getting new values while we have an illegal port number or the port
+ // number is already in use.
+ while ((result == 0) || (FindPort(result) >= 0)) {
hausner 2014/03/28 23:27:30 You could turn this into a do { } while() loop.
Ivan Posva 2014/03/28 23:39:26 I tried to write it as an repeat-until, but that s
+ result = prng_->NextUInt32() & kMASK;
+ }
ASSERT(result != 0);
+ ASSERT(FindPort(result) < 0);
return result;
}
@@ -258,6 +260,7 @@
void PortMap::InitOnce() {
mutex_ = new Mutex();
+ prng_ = new Random();
static const intptr_t kInitialCapacity = 8;
// TODO(iposva): Verify whether we want to keep exponentially growing.
« no previous file with comments | « runtime/vm/port.h ('k') | runtime/vm/random.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698