Commit cf5e25a9 authored by Juha Reunanen's avatar Juha Reunanen Committed by Davis E. King
Browse files

Problem: integer overflow when calculating sizes (may happen e.g. with very large images) (#1148)

* Problem: integer overflow when calculating sizes (may happen e.g. with very large images)
Solution: change some types from (unsigned) long to size_t

# Conflicts:
#	dlib/dnn/tensor.h

* Fix the fact that std::numeric_limits<unsigned long>::max() isn't always the same number

* Revert serialization changes

* Review fix: use long long instead of size_t

* From long to long long all the way

* Change more types to (hopefully) make the compiler happy

* Change many more types to size_t

* Change even more types to size_t

* Minor type changes
parent 1cf6dbf4
......@@ -106,7 +106,7 @@ namespace dlib
}
explicit array (
unsigned long new_size
size_t new_size
) :
array_size(0),
max_array_size(0),
......@@ -125,22 +125,22 @@ namespace dlib
);
inline const T& operator[] (
unsigned long pos
size_t pos
) const;
inline T& operator[] (
unsigned long pos
size_t pos
);
void set_size (
unsigned long size
size_t size
);
inline unsigned long max_size(
inline size_t max_size(
) const;
void set_max_size(
unsigned long max
size_t max
);
void swap (
......@@ -148,7 +148,7 @@ namespace dlib
);
// functions from the enumerable interface
inline unsigned long size (
inline size_t size (
) const;
inline bool at_start (
......@@ -173,7 +173,7 @@ namespace dlib
);
void resize (
unsigned long new_size
size_t new_size
);
const T& back (
......@@ -209,8 +209,8 @@ namespace dlib
typename mem_manager::template rebind<T>::other pool;
// data members
unsigned long array_size;
unsigned long max_array_size;
size_t array_size;
size_t max_array_size;
T* array_elements;
mutable T* pos;
......@@ -248,7 +248,7 @@ namespace dlib
serialize(item.max_size(),out);
serialize(item.size(),out);
for (unsigned long i = 0; i < item.size(); ++i)
for (size_t i = 0; i < item.size(); ++i)
serialize(item[i],out);
}
catch (serialization_error e)
......@@ -268,12 +268,12 @@ namespace dlib
{
try
{
unsigned long max_size, size;
size_t max_size, size;
deserialize(max_size,in);
deserialize(size,in);
item.set_max_size(max_size);
item.set_size(size);
for (unsigned long i = 0; i < size; ++i)
for (size_t i = 0; i < size; ++i)
deserialize(item[i],in);
}
catch (serialization_error e)
......@@ -333,7 +333,7 @@ namespace dlib
>
const T& array<T,mem_manager>::
operator[] (
unsigned long pos
size_t pos
) const
{
// make sure requires clause is not broken
......@@ -356,7 +356,7 @@ namespace dlib
>
T& array<T,mem_manager>::
operator[] (
unsigned long pos
size_t pos
)
{
// make sure requires clause is not broken
......@@ -379,7 +379,7 @@ namespace dlib
>
void array<T,mem_manager>::
set_size (
unsigned long size
size_t size
)
{
// make sure requires clause is not broken
......@@ -405,7 +405,7 @@ namespace dlib
typename T,
typename mem_manager
>
unsigned long array<T,mem_manager>::
size_t array<T,mem_manager>::
size (
) const
{
......@@ -420,7 +420,7 @@ namespace dlib
>
void array<T,mem_manager>::
set_max_size(
unsigned long max
size_t max
)
{
reset();
......@@ -458,7 +458,7 @@ namespace dlib
typename T,
typename mem_manager
>
unsigned long array<T,mem_manager>::
size_t array<T,mem_manager>::
max_size (
) const
{
......@@ -476,8 +476,8 @@ namespace dlib
array<T,mem_manager>& item
)
{
unsigned long array_size_temp = item.array_size;
unsigned long max_array_size_temp = item.max_array_size;
auto array_size_temp = item.array_size;
auto max_array_size_temp = item.max_array_size;
T* array_elements_temp = item.array_elements;
item.array_size = array_size;
......@@ -646,7 +646,7 @@ namespace dlib
>
void array<T,mem_manager>::
resize (
unsigned long new_size
size_t new_size
)
{
if (this->max_size() < new_size)
......@@ -654,7 +654,7 @@ namespace dlib
array temp;
temp.set_max_size(new_size);
temp.set_size(new_size);
for (unsigned long i = 0; i < this->size(); ++i)
for (size_t i = 0; i < this->size(); ++i)
{
exchange((*this)[i],temp[i]);
}
......@@ -769,7 +769,7 @@ namespace dlib
array temp;
temp.set_max_size(this->size()*2 + 1);
temp.set_size(this->size()+1);
for (unsigned long i = 0; i < this->size(); ++i)
for (size_t i = 0; i < this->size(); ++i)
{
exchange((*this)[i],temp[i]);
}
......
......@@ -66,7 +66,7 @@ namespace dlib
!*/
explicit array (
unsigned long new_size
size_t new_size
);
/*!
ensures
......@@ -116,7 +116,7 @@ namespace dlib
!*/
const T& operator[] (
unsigned long pos
size_t pos
) const;
/*!
requires
......@@ -126,7 +126,7 @@ namespace dlib
!*/
T& operator[] (
unsigned long pos
size_t pos
);
/*!
requires
......@@ -136,7 +136,7 @@ namespace dlib
!*/
void set_size (
unsigned long size
size_t size
);
/*!
requires
......@@ -155,7 +155,7 @@ namespace dlib
if it does throw then the call to set_size() has no effect
!*/
unsigned long max_size(
size_t max_size(
) const;
/*!
ensures
......@@ -163,7 +163,7 @@ namespace dlib
!*/
void set_max_size(
unsigned long max
size_t max
);
/*!
ensures
......@@ -198,7 +198,7 @@ namespace dlib
!*/
void resize (
unsigned long new_size
size_t new_size
);
/*!
ensures
......
......@@ -312,8 +312,8 @@ namespace dlib
}
}
unsigned long size (
) const { return static_cast<unsigned long>(nc_ * nr_); }
size_t size (
) const { return static_cast<size_t>(nc_) * static_cast<size_t>(nr_); }
long width_step (
) const
......
......@@ -356,7 +356,7 @@ namespace dlib
table.clear();
}
unsigned long size () const { return table.size(); }
size_t size () const { return table.size(); }
bool move_next() const { return table.move_next(); }
void reset() const { table.reset(); }
map_pair<assignment,double>& element()
......
......@@ -168,7 +168,7 @@ namespace dlib
);
// functions from the enumerable interface
inline unsigned long size (
inline size_t size (
) const;
bool at_start (
......@@ -597,7 +597,7 @@ namespace dlib
typename mem_manager,
typename compare
>
unsigned long binary_search_tree_kernel_1<domain,range,mem_manager,compare>::
size_t binary_search_tree_kernel_1<domain,range,mem_manager,compare>::
size (
) const
{
......
......@@ -169,7 +169,7 @@ namespace dlib
);
// functions from the enumerable interface
inline unsigned long size (
inline size_t size (
) const;
bool at_start (
......@@ -543,7 +543,7 @@ namespace dlib
typename mem_manager,
typename compare
>
unsigned long binary_search_tree_kernel_2<domain,range,mem_manager,compare>::
size_t binary_search_tree_kernel_2<domain,range,mem_manager,compare>::
size (
) const
{
......
......@@ -305,7 +305,7 @@ namespace dlib
bool move_next (
) const { return options.move_next(); }
unsigned long size (
size_t size (
) const { return options.size(); }
private:
......
......@@ -34,7 +34,7 @@ namespace dlib
}
}
unsigned long size (
size_t size (
) const noexcept
{
return items.size();
......
......@@ -44,7 +44,7 @@ namespace dlib
(i.e. this object contains new_size subsets, each containing exactly one element)
!*/
unsigned long size (
size_t size (
) const noexcept;
/*!
ensures
......
......@@ -34,7 +34,7 @@ namespace dlib
number_of_sets = new_size;
}
unsigned long size (
size_t size (
) const noexcept
{
return disjoint_subsets_.size();
......
......@@ -50,7 +50,7 @@ namespace dlib
- #get_size_of_set(i) == 1
!*/
unsigned long size (
size_t size (
) const noexcept;
/*!
ensures
......
......@@ -1639,17 +1639,17 @@ namespace dlib
float* g = grad.host();
const float x_scale = (grad.nc()-1)/(float)std::max<long>((gradient_input.nc()-1),1);
const float y_scale = (grad.nr()-1)/(float)std::max<long>((gradient_input.nr()-1),1);
for (long samp = 0; samp < gradient_input.num_samples(); ++samp)
for (long long samp = 0; samp < gradient_input.num_samples(); ++samp)
{
for (long k = 0; k < gradient_input.k(); ++k)
for (long long k = 0; k < gradient_input.k(); ++k)
{
for (long r = 0; r < gradient_input.nr(); ++r)
for (long long r = 0; r < gradient_input.nr(); ++r)
{
const float y = r*y_scale;
const long long top = static_cast<long long>(std::floor(y));
const long long bottom = std::min(top+1, grad.nr()-1);
const float tb_frac = y - top;
for (long c = 0; c < gradient_input.nc(); ++c)
for (long long c = 0; c < gradient_input.nc(); ++c)
{
const float x = c*x_scale;
const long long left = static_cast<long long>(std::floor(x));
......
......@@ -467,7 +467,7 @@ namespace dlib
!*/
explicit resizable_tensor(
long long n_, long long g k_ = 1, long long nr_ = 1, long long nc_ = 1
long long n_, long long k_ = 1, long long nr_ = 1, long long nc_ = 1
);
/*!
requires
......
......@@ -125,7 +125,7 @@ namespace dlib
return false;
}
unsigned long size (
size_t size (
) const
{
return rect.area() - inner_rect.area();
......
......@@ -99,7 +99,7 @@ namespace dlib
- returns false if there are no more elements in the container
!*/
unsigned long size (
size_t size (
) const;
/*!
ensures
......
......@@ -1399,7 +1399,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
unsigned long widget_group::
size_t widget_group::
size (
) const
{
......@@ -1578,7 +1578,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
unsigned long popup_menu::
size_t popup_menu::
size (
) const
{
......
......@@ -329,7 +329,7 @@ namespace dlib
const drawable& widget
);
unsigned long size (
size_t size (
) const;
void set_pos (
......@@ -2039,7 +2039,7 @@ namespace dlib
unsigned long idx
);
unsigned long size (
size_t size (
) const;
void clear (
......
......@@ -967,7 +967,7 @@ namespace dlib
widgets in this group and the upper left corner of get_rect().
!*/
unsigned long size (
size_t size (
) const;
/*!
ensures
......@@ -1561,7 +1561,7 @@ namespace dlib
- the menu_item in this with the index idx has been disabled
!*/
unsigned long size (
size_t size (
) const;
/*!
ensures
......
......@@ -2470,7 +2470,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <typename S>
unsigned long list_box<S>::
size_t list_box<S>::
size (
) const
{
......
......@@ -1792,7 +1792,7 @@ namespace dlib
bool move_next (
) const;
unsigned long size (
size_t size (
) const;
unsigned long get_selected (
......
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