boostorg / boostorg/integer

undefined behavior in gcd when parameters are minimum values of signed integers

Đang mở
#33 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
C++
Star
19
Fork
58
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

This is being reported to me via a clang-analyzer check, and it refers to the gcd template function in common_factor_rt.hpp

Integer template parameter type is a signed integer, and both parameters ('a' and 'b') are std::numeric_limits\::min.

```
444 template
445 inline BOOST_CXX14_CONSTEXPR Integer gcd(Integer const &a, Integer const &b) BOOST_GCD_NOEXCEPT(Integer)
446 {
447 if(a == (std::numeric_limits::min)())
448 return a == static_cast(0) ? gcd_detail::gcd_traits::abs(b) : boost::integer::gcd(static_cast(a % b), b);
449 else if (b == (std::numeric_limits::min)())
450 return b == static_cast(0) ? gcd_detail::gcd_traits::abs(a) : boost::integer::gcd(a, static_cast(b % a));
451 return gcd_detail::optimal_gcd_select(static_cast(gcd_detail::gcd_traits::abs(a)), static_cast(gcd_detail::gcd_traits::abs(b)));
452 }
```
In the initial invocation, the condition on 447 is true, and the condition on 448 is false. This leads to a recursive call to the function with parameters a=0 and b=std::numeric_limits\::min.

In the recursive call, the condition on 447 is false, the condition on 449 is true, and the condition on 450 is false. Another recursive call is on-deck with a=0 and b=b%a, But a is zero at this point. Anything mod 0 is undefined behavior.

My suggested fix is to check if a and b are the same value at the start of the function, and return a right away.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu trong common_factor_rt.hpp, tại template gcd quanh các dòng 444-451, và tái hiện lời gọi bằng cách đặt cả hai tham số có dấu thành std::numeric_limits::min(). Theo dõi đường đệ quy và xác minh rằng trường hợp giá trị nhỏ nhất không còn đi tới phép modulo cho zero. Thêm hoặc cập nhật coverage hồi quy nếu repository cung cấp vị trí kiểm thử gcd phù hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
cpp
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.