Skip to content

Commit ceeae42

Browse files
Merge pull request #19 from Neko-Box-Coder/HashCollision
Fixing hash collision not working
2 parents 3ca21f5 + bc2c5aa commit ceeae42

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

Src/UnitTests/BuildsManagerTest.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ int main(int argc, char** argv)
330330
ssTEST_OUTPUT_SETUP
331331
(
332332
std::vector<std::shared_ptr<OverrideResult>> initializeResults(4, nullptr);
333+
334+
//NOTE: This initializes the mapping for the first 2 scripts in scriptsPaths
333335
prepareInitialization( initializeResults.at(0), initializeResults.at(1),
334336
initializeResults.at(2), initializeResults.at(3), "");
335337
CO_SETUP_OVERRIDE (OverrideInstance, Mock_exists)
@@ -352,7 +354,7 @@ int main(int argc, char** argv)
352354
//Hash script path
353355
std::shared_ptr<OverrideResult> hashResult = CreateOverrideResult();
354356
CO_SETUP_OVERRIDE (OverrideInstance, operator())
355-
.WhenCalledWith<std::string>(scriptsPaths.at(2))
357+
.WhenCalledWith<std::string>(scriptsPaths.at(2) + "0")
356358
.Returns<std::size_t>(15)
357359
.Times(1)
358360
.AssignResult(hashResult);
@@ -386,6 +388,9 @@ int main(int argc, char** argv)
386388
ssTEST_OUTPUT_ASSERT( "New mapping exists",
387389
BuildsManagerAccessor ::GetMappings(*buildsManager)
388390
.count(scriptsPaths.at(2)) == 1);
391+
ssTEST_OUTPUT_ASSERT( "New mapping value is correct",
392+
BuildsManagerAccessor ::GetMappings(*buildsManager)
393+
.at(scriptsPaths.at(2)) == scriptsBuildsPaths.at(2));
389394
};
390395

391396
ssTEST("CreateBuildMapping Should Create Build Mapping When Hashed Output Is Not Unique")
@@ -394,17 +399,17 @@ int main(int argc, char** argv)
394399
commonInitializeBuildsManager();
395400
ssTEST_OUTPUT_SETUP
396401
(
397-
//Hash script path not unique
402+
//Hash script path not unique (first attempt)
398403
std::shared_ptr<OverrideResult> hashNotUniqueResult = CreateOverrideResult();
399404
CO_SETUP_OVERRIDE (OverrideInstance, operator())
400-
.WhenCalledWith(scriptsPaths.at(2))
405+
.WhenCalledWith(scriptsPaths.at(2) + "0")
401406
.Returns<std::size_t>(10)
402407
.Times(1)
403408
.AssignResult(hashNotUniqueResult);
404-
//Hash script path unique
409+
//Hash script path unique (second attempt)
405410
std::shared_ptr<OverrideResult> hashUniqueResult = CreateOverrideResult();
406411
CO_SETUP_OVERRIDE (OverrideInstance, operator())
407-
.WhenCalledWith(scriptsPaths.at(2))
412+
.WhenCalledWith(scriptsPaths.at(2) + "1")
408413
.Returns<std::size_t>(15)
409414
.Times(1)
410415
.AssignResult(hashUniqueResult);
@@ -419,13 +424,16 @@ int main(int argc, char** argv)
419424
);
420425
ssTEST_OUTPUT_ASSERT( "CreateBuildMapping should succeed",
421426
buildsManager->CreateBuildMapping(scriptsPaths.at(2)), true);
422-
ssTEST_OUTPUT_ASSERT( "Hash script path not unique",
427+
ssTEST_OUTPUT_ASSERT( "Hash script path not unique (first attempt)",
423428
hashNotUniqueResult->GetSucceedCount() == 1);
424-
ssTEST_OUTPUT_ASSERT( "Hash script path unique",
429+
ssTEST_OUTPUT_ASSERT( "Hash script path unique (second attempt)",
425430
hashUniqueResult->LastStatusSucceed());
426431
ssTEST_OUTPUT_ASSERT( "New mapping exists",
427432
BuildsManagerAccessor ::GetMappings(*buildsManager)
428433
.count(scriptsPaths.at(2)) == 1);
434+
ssTEST_OUTPUT_ASSERT( "New mapping value is correct",
435+
BuildsManagerAccessor ::GetMappings(*buildsManager)
436+
.at(scriptsPaths.at(2)) == scriptsBuildsPaths.at(2));
429437
};
430438

431439
ssTEST("CreateBuildMapping Should Return True When Mapping For Script Exists")
@@ -630,3 +638,4 @@ int main(int argc, char** argv)
630638
}
631639

632640

641+

Src/runcpp2/BuildsManager.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,28 @@ namespace runcpp2
171171
return true;
172172

173173
//Create build folder for script
174-
constexpr int MAX_TRIES = 16;
174+
constexpr int MAX_TRIES = 1000;
175175
std::size_t scriptPathHash;
176176
std::string scriptBuildPathStr;
177-
int i = 0;
177+
int counter = 0;
178178
do
179179
{
180-
scriptPathHash = std::hash<std::string>{}(processScriptPathStr);
180+
scriptPathHash = std::hash<std::string>{}(processScriptPathStr + std::to_string(counter));
181181
scriptBuildPathStr = "./" + std::to_string(scriptPathHash);
182182

183183
if(ReverseMappings.count(scriptBuildPathStr) == 0)
184184
break;
185-
else if(i == MAX_TRIES - 1)
186-
{
187-
ssLOG_ERROR("Failed to get unique hash for " << scriptPath.string());
188-
return false;
189-
}
185+
186+
counter++;
187+
}
188+
while(counter < MAX_TRIES);
189+
190+
if(counter == MAX_TRIES)
191+
{
192+
ssLOG_ERROR("Failed to get unique hash for " << scriptPath.string() <<
193+
" after " << MAX_TRIES << " attempts");
194+
return false;
190195
}
191-
while(i++ < MAX_TRIES);
192196

193197
ghc::filesystem::path scriptBuildPath = BuildDirectory / scriptBuildPathStr;
194198
std::error_code e;

0 commit comments

Comments
 (0)