Skip to content

Commit

Permalink
Fix Integer Overflow #2357 (#2400)
Browse files Browse the repository at this point in the history
* Fix integer overflow #2357

* Use 2 spaces not 4

* Move constant the config.h
Move check outside the max_depth check

* Move the max leaves check to config.h

* Remove unnecessary check
  • Loading branch information
chris-smith-zocdoc authored and guolinke committed Sep 27, 2019
1 parent d064019 commit 1f88721
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/Parameters.rst
Expand Up @@ -135,7 +135,7 @@ Core Parameters

- in ``dart``, it also affects on normalization weights of dropped trees

- ``num_leaves`` :raw-html:`<a id="num_leaves" title="Permalink to this parameter" href="#num_leaves">&#x1F517;&#xFE0E;</a>`, default = ``31``, type = int, aliases: ``num_leaf``, ``max_leaves``, ``max_leaf``, constraints: ``num_leaves > 1``
- ``num_leaves`` :raw-html:`<a id="num_leaves" title="Permalink to this parameter" href="#num_leaves">&#x1F517;&#xFE0E;</a>`, default = ``31``, type = int, aliases: ``num_leaf``, ``max_leaves``, ``max_leaf``, constraints: ``1 < num_leaves <= 131072``

- max number of leaves in one tree

Expand Down
1 change: 1 addition & 0 deletions include/LightGBM/config.h
Expand Up @@ -166,6 +166,7 @@ struct Config {
// default = 31
// alias = num_leaf, max_leaves, max_leaf
// check = >1
// check = <=131072
// desc = max number of leaves in one tree
int num_leaves = kDefaultNumLeaves;

Expand Down
8 changes: 6 additions & 2 deletions src/io/config.cpp
Expand Up @@ -320,12 +320,16 @@ void Config::CheckParamConflict() {
}
// Check max_depth and num_leaves
if (max_depth > 0) {
int full_num_leaves = static_cast<int>(std::pow(2, max_depth));
double full_num_leaves = std::pow(2, max_depth);
if (full_num_leaves > num_leaves
&& num_leaves == kDefaultNumLeaves) {
Log::Warning("Accuracy may be bad since you didn't set num_leaves and 2^max_depth > num_leaves");
}
num_leaves = std::min(num_leaves, 2 << max_depth);

if (full_num_leaves < num_leaves) {
// Fits in an int, and is more restrictive than the current num_leaves
num_leaves = static_cast<int>(full_num_leaves);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/io/config_auto.cpp
Expand Up @@ -301,6 +301,7 @@ void Config::GetMembersFromString(const std::unordered_map<std::string, std::str

GetInt(params, "num_leaves", &num_leaves);
CHECK(num_leaves >1);
CHECK(num_leaves <=131072);

GetInt(params, "num_threads", &num_threads);

Expand Down

0 comments on commit 1f88721

Please sign in to comment.