The following issues were found

c10/util/Logging.cpp
1 issues
getenv - Environment variables are untrustable input if they can be set by an attacker. They can have any content and length, and the same variable can be set more than once
Security

Line: 93 Column: 21 CWE codes: 807 20
Suggestion: Check environment variables carefully before using them

              
namespace {
bool IsAPIUsageDebugMode() {
  const char* val = getenv("PYTORCH_API_USAGE_STDERR");
  return val && *val; // any non-empty value
}

void APIUsageDebug(const string& event) {
  // use stderr to avoid messing with glog

            

Reported by FlawFinder.

c10/util/LeftRight.h
1 issues
read - Check buffer boundaries if used in a loop including recursive loops
Security

Line: 79 Column: 8 CWE codes: 120 20

                }

  template <typename F>
  auto read(F&& readFunc) const -> typename std::result_of<F(const T&)>::type {
    detail::IncrementRAII _increment_counter(
        &_counters[_foregroundCounterIndex.load()]);

    return readFunc(_data[_foregroundDataIndex.load()]);
  }

            

Reported by FlawFinder.

c10/util/BFloat16.h
1 issues
memcpy - Does not check for buffer overflows when copying to destination
Security

Line: 30 Column: 8 CWE codes: 120
Suggestion: Make sure destination can always hold the source data

                tempRes = reinterpret_cast<float*>(&tmp);
  res = *tempRes;
#else
  std::memcpy(&res, &tmp, sizeof(tmp));
#endif

  return res;
}


            

Reported by FlawFinder.

c10/util/ArrayRef.h
1 issues
equal - Function does not check the second iterator for over-read conditions
Security

Line: 192 Column: 41 CWE codes: 126
Suggestion: This function is often discouraged by most C++ coding standards in favor of its safer alternatives provided since C++14. Consider using a form of this function that checks the second iterator before potentially overflowing it

              
  /// equals - Check for element-wise equality.
  constexpr bool equals(ArrayRef RHS) const {
    return Length == RHS.Length && std::equal(begin(), end(), RHS.begin());
  }

  /// slice(n, m) - Take M elements of the array starting at element N
  C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA ArrayRef<T> slice(size_t N, size_t M)
      const {

            

Reported by FlawFinder.

c10/test/util/typeid_test.cpp
1 issues
syntax error
Error

Line: 30

                EXPECT_EQ(TypeMeta::Id<TypeMetaTestFoo>(), TypeMeta::Id<TypeMetaTestFoo>());
}

TEST(TypeMetaTest, Names) {
  TypeMeta null_meta;
  EXPECT_EQ("nullptr (uninitialized)", null_meta.name());
  TypeMeta int_meta = TypeMeta::Make<int>();
  EXPECT_EQ("int", int_meta.name());
  TypeMeta string_meta = TypeMeta::Make<string>();

            

Reported by Cppcheck.

c10/test/util/registry_test.cpp
1 issues
syntax error
Error

Line: 39

              };
REGISTER_FOO(AnotherBar);

TEST(RegistryTest, CanRunCreator) {
  std::unique_ptr<Foo> bar(FooRegistry()->Create("Bar", 1));
  EXPECT_TRUE(bar != nullptr) << "Cannot create bar.";
  std::unique_ptr<Foo> another_bar(FooRegistry()->Create("AnotherBar", 1));
  EXPECT_TRUE(another_bar != nullptr);
}

            

Reported by Cppcheck.

c10/test/util/ordered_preserving_dict_test.cpp
1 issues
syntax error
Error

Line: 64

                return dict;
}

TEST(OrderedPreservingDictTest, InsertAndDeleteBasic) {
  dict_int_int dict;
  test_dict(dict);
  dict.clear();
  test_dict(dict);
}

            

Reported by Cppcheck.

c10/test/util/optional_test.cpp
1 issues
syntax error
Error

Line: 68

              
TYPED_TEST_CASE(OptionalTest, OptionalTypes);

TYPED_TEST(OptionalTest, Empty) {
  typename TestFixture::optional empty;

  EXPECT_FALSE((bool)empty);
  EXPECT_FALSE(empty.has_value());


            

Reported by Cppcheck.

c10/test/util/logging_test.cpp
1 issues
syntax error
Error

Line: 35

                int y = 5;
  int z = 0;
  try {
    CAFFE_ENFORCE_THAT(std::equal_to<void>(), ==, ++x, ++y, "Message: ", z++);
    // This should never be triggered.
    ADD_FAILURE();
  } catch (const ::c10::Error& err) {
    auto errStr = std::string(err.what());
    EXPECT_NE(errStr.find("5 vs 6"), string::npos);

            

Reported by Cppcheck.

c10/test/util/bfloat16_test.cpp
1 issues
syntax error
Error

Line: 24

                return res;
}

TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
  // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
  float in[100];
  for (int i = 0; i < 100; ++i) {
    // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers)
    in[i] = i + 1.25;

            

Reported by Cppcheck.