The following issues were found

caffe2/video/video_decoder.cc
4 issues
memcpy - Does not check for buffer overflows when copying to destination
Security

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

                    out_samples = swr_convert(swr, &output, out_samples, input, in_samples);
      auto sample_size = out_samples * c->channels * sizeof(float);
      auto buffer = std::make_unique<float[]>(sample_size);
      memcpy(buffer.get(), output, sample_size);
      av_freep(&output);

      unique_ptr<DecodedAudio> audio_sample = make_unique<DecodedAudio>();
      audio_sample->dataSize_ = data_size;
      audio_sample->outSampleSize_ = out_samples * c->channels;

            

Reported by FlawFinder.

memcpy - Does not check for buffer overflows when copying to destination
Security

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

                    unsigned char* buffer_rgb_ptr = new unsigned char[clip_size];
      int clip_start = clip_start_positions[i];
      for (int j = 0; j < params.num_of_required_frame_; j++) {
        memcpy(
            buffer_rgb_ptr + j * image_size,
            (unsigned char*)sampledFrames[j + clip_start]->data_.get(),
            image_size * sizeof(unsigned char));
      }
      buffer_rgb.push_back(buffer_rgb_ptr);

            

Reported by FlawFinder.

memcpy - Does not check for buffer overflows when copying to destination
Security

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

                    unsigned char* buffer_rgb_ptr = new unsigned char[clip_size];
      int clip_start = floor(i * sample_stepsz);
      for (int j = 0; j < params.num_of_required_frame_; j++) {
        memcpy(
            buffer_rgb_ptr + j * image_size,
            (unsigned char*)sampledFrames[j + clip_start]->data_.get(),
            image_size * sizeof(unsigned char));
      }
      buffer_rgb.push_back(buffer_rgb_ptr);

            

Reported by FlawFinder.

read - Check buffer boundaries if used in a loop including recursive loops
Security

Line: 149 Column: 21 CWE codes: 120 20

                  int probeSz = 1 * 1024 + AVPROBE_PADDING_SIZE;
    DecodedFrame::AvDataPtr probe((uint8_t*)av_malloc(probeSz));
    memset(probe.get(), 0, probeSz);
    int len = ioctx.read(probe.get(), probeSz - AVPROBE_PADDING_SIZE);
    if (len < probeSz - AVPROBE_PADDING_SIZE) {
      LOG(ERROR) << "Insufficient data to determine video format";
      return;
    }
    // seek back to start of stream

            

Reported by FlawFinder.

test/distributed/launcher/bin/test_script.py
4 issues
Missing module docstring
Error

Line: 1 Column: 1

              #!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import argparse

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 14 Column: 1

              from pathlib import Path


def parse_args():
    parser = argparse.ArgumentParser(description="test script")

    parser.add_argument(
        "--fail",
        default=False,

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 33 Column: 1

                  return parser.parse_args()


def main():
    args = parse_args()
    env_vars = [
        "LOCAL_RANK",
        "RANK",
        "GROUP_RANK",

            

Reported by Pylint.

Unnecessary "else" after "raise"
Error

Line: 59 Column: 5

                      value = os.environ[env_var]
        print(f"{env_var} = {value}")

    if args.fail:
        raise RuntimeError("raising exception since --fail flag was set")
    else:
        file = os.path.join(args.touch_file_dir, os.environ["RANK"])
        Path(file).touch()
        print(f"Success, created {file}")

            

Reported by Pylint.

test/cpp/api/serialize.cpp
4 issues
memcpy - Does not check for buffer overflows when copying to destination
Security

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

                  if (pos >= serialized.size()) return 0;
    size_t nbytes = std::min(static_cast<size_t>(pos) + n,
                             serialized.size()) - pos;
    memcpy(buf, serialized.data() + pos, nbytes);
    return nbytes;
  },
  [&]() -> size_t { return serialized.size(); });
  ASSERT_TRUE(z.defined());
  ASSERT_EQ(x.sizes().vec(), z.sizes().vec());

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 45 Column: 24 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

              
  ASSERT_TRUE(lhs_params.size() == rhs_params.size());
  for (size_t j = 0; j < lhs_params.size(); j++) {
    ASSERT_TRUE(torch::equal(lhs_params[j], rhs_params[j]));
  }
  ASSERT_TRUE(static_cast<const DerivedOptions&>(lhs.options()) == static_cast<const DerivedOptions&>(rhs.options()));
}

template <typename DerivedOptimizerParamState>

            

Reported by FlawFinder.

read - Check buffer boundaries if used in a loop including recursive loops
Security

Line: 901 Column: 17 CWE codes: 120 20

                torch::serialize::InputArchive input_archive;
  input_archive.load_from(tempfile.name);
  c10::IValue ivalue_out;
  input_archive.read("value", ivalue_out);
  ASSERT_EQ(ivalue_out.toInt(), 1);

  ASSERT_THROWS_WITH(input_archive.read("bad_key", ivalue_out), "does not have a field with name");
}


            

Reported by FlawFinder.

read - Check buffer boundaries if used in a loop including recursive loops
Security

Line: 904 Column: 36 CWE codes: 120 20

                input_archive.read("value", ivalue_out);
  ASSERT_EQ(ivalue_out.toInt(), 1);

  ASSERT_THROWS_WITH(input_archive.read("bad_key", ivalue_out), "does not have a field with name");
}

// NOTE: if a `Module` contains unserializable submodules (e.g. `nn::Functional`),
// we expect those submodules to be skipped when the `Module` is being serialized.
TEST(SerializeTest, UnserializableSubmoduleIsSkippedWhenSavingModule) {

            

Reported by FlawFinder.

caffe2/utils/math/reduce.cc
4 issues
memcpy - Does not check for buffer overflows when copying to destination
Security

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

                    const T* X,                                                 \
      T* Y,                                                       \
      CPUContext* context) {                                      \
    std::memcpy(Y, X, sizeof(T) * cols);                          \
    for (int i = 1; i < rows; ++i) {                              \
      MathFunc<T, CPUContext>(cols, Y, X + i * cols, Y, context); \
    }                                                             \
    Scale<T, T, CPUContext>(cols, alpha, Y, Y, context);          \
  }

            

Reported by FlawFinder.

memcpy - Does not check for buffer overflows when copying to destination
Security

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

                  return;
  }
  if (std::equal(X_dims, X_dims + ndim, Y_dims)) {
    std::memcpy(mean, X, sizeof(T) * Y_size);
    std::memset(var, 0, sizeof(T) * Y_size);
    return;
  }
  // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
  int rows;

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 516 Column: 12 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

                  std::memset(var, 0, sizeof(T) * Y_size);
    return;
  }
  if (std::equal(X_dims, X_dims + ndim, Y_dims)) {
    std::memcpy(mean, X, sizeof(T) * Y_size);
    std::memset(var, 0, sizeof(T) * Y_size);
    return;
  }
  // NOLINTNEXTLINE(cppcoreguidelines-init-variables)

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 602 Column: 14 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

                    std::memset(Y, 0, sizeof(T) * Y_size);                               \
      return;                                                              \
    }                                                                      \
    if (std::equal(X_dims, X_dims + ndim, Y_dims)) {                       \
      if (kIsNorm) {                                                       \
        EigenVectorArrayMap<T>(Y, Y_size) =                                \
            ConstEigenVectorArrayMap<T>(X, X_size).abs() * alpha;          \
      } else {                                                             \
        Scale<T, T, CPUContext>(Y_size, alpha, X, Y, context);             \

            

Reported by FlawFinder.

caffe2/python/operator_test/sparse_lengths_sum_benchmark.py
4 issues
Missing module docstring
Error

Line: 1 Column: 1

              

import argparse
import datetime

import numpy as np
from caffe2.python import core, workspace



            

Reported by Pylint.

Too many arguments (7/5)
Error

Line: 18 Column: 1

              }


def benchmark_sparse_lengths_sum(
    dtype_str,
    categorical_limit,
    embedding_size,
    average_len,
    batch_size,

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 18 Column: 1

              }


def benchmark_sparse_lengths_sum(
    dtype_str,
    categorical_limit,
    embedding_size,
    average_len,
    batch_size,

            

Reported by Pylint.

Function name "f" doesn't conform to snake_case naming style
Error

Line: 46 Column: 5

              
    # In order to produce truly random lengths and indices, we will embed a
    # Python operator in the net to generate them.
    def f(_, outputs):
        lengths = np.random.randint(
            int(np.round(average_len * 0.75)),
            int(np.round(average_len * 1.25)) + 1,
            batch_size,
        ).astype(np.int32)

            

Reported by Pylint.

caffe2/python/test/gpu_context_test.py
4 issues
Missing module docstring
Error

Line: 1 Column: 1

              




import unittest

import torch
from caffe2.python import core, workspace

            

Reported by Pylint.

Missing class docstring
Error

Line: 15 Column: 1

              # initialization and thus we should be the ones calling GlobalInit
@unittest.skipIf(not workspace.has_cuda_support,
                 "THC pool testing is obscure and doesn't work on HIP yet")
class TestGPUInit(unittest.TestCase):
    def testTHCAllocator(self):
        cuda_or_hip = 'hip' if workspace.has_hip_support else 'cuda'
        flag = '--caffe2_{}_memory_pool=thc'.format(cuda_or_hip)
        core.GlobalInit(['caffe2', flag])
        # just run one operator

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 16 Column: 5

              @unittest.skipIf(not workspace.has_cuda_support,
                 "THC pool testing is obscure and doesn't work on HIP yet")
class TestGPUInit(unittest.TestCase):
    def testTHCAllocator(self):
        cuda_or_hip = 'hip' if workspace.has_hip_support else 'cuda'
        flag = '--caffe2_{}_memory_pool=thc'.format(cuda_or_hip)
        core.GlobalInit(['caffe2', flag])
        # just run one operator
        # it's importantant to not call anything here from Torch API

            

Reported by Pylint.

Method name "testTHCAllocator" doesn't conform to snake_case naming style
Error

Line: 16 Column: 5

              @unittest.skipIf(not workspace.has_cuda_support,
                 "THC pool testing is obscure and doesn't work on HIP yet")
class TestGPUInit(unittest.TestCase):
    def testTHCAllocator(self):
        cuda_or_hip = 'hip' if workspace.has_hip_support else 'cuda'
        flag = '--caffe2_{}_memory_pool=thc'.format(cuda_or_hip)
        core.GlobalInit(['caffe2', flag])
        # just run one operator
        # it's importantant to not call anything here from Torch API

            

Reported by Pylint.

mypy_plugins/check_mypy_version.py
4 issues
Unable to import 'mypy.plugin'
Error

Line: 5 Column: 1

              import sys
from pathlib import Path

from mypy.plugin import Plugin


def get_correct_mypy_version():
    # there's probably a more elegant way to do this
    match, = re.finditer(

            

Reported by Pylint.

Missing module docstring
Error

Line: 1 Column: 1

              import re
import sys
from pathlib import Path

from mypy.plugin import Plugin


def get_correct_mypy_version():
    # there's probably a more elegant way to do this

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 8 Column: 1

              from mypy.plugin import Plugin


def get_correct_mypy_version():
    # there's probably a more elegant way to do this
    match, = re.finditer(
        r'mypy==(\d+(?:\.\d+)*)',
        Path('.circleci/docker/common/install_conda.sh').read_text(),
    )

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 18 Column: 1

                  return version


def plugin(version: str):
    correct_version = get_correct_mypy_version()
    if version != correct_version:
        print(f'''\
You are using mypy version {version}, which is not supported
in the PyTorch repo. Please switch to mypy version {correct_version}.

            

Reported by Pylint.

test/cpp/api/tensor_indexing.cpp
4 issues
equal - Function does not check the second iterator for over-read conditions
Security

Line: 34 Column: 24 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

                  ASSERT_TRUE(indices[5].slice().stop() == INDEX_MAX);
    ASSERT_TRUE(indices[5].slice().step() == 2);
    ASSERT_TRUE(indices[6].is_tensor());
    ASSERT_TRUE(torch::equal(indices[6].tensor(), torch::tensor({1, 2})));
  }

  ASSERT_THROWS_WITH(
    TensorIndex(".."),
    "Expected \"...\" to represent an ellipsis index, but got \"..\"");

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 92 Column: 24 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

                  torch::Tensor index = torch::arange(10, torch::kLong).cpu();
    torch::Tensor result = at::index(tensor, {index});
    torch::Tensor result_with_init_list = tensor.index({index});
    ASSERT_TRUE(result.equal(result_with_init_list));
  }
  {
    torch::Tensor tensor = torch::randn({20, 20});
    torch::Tensor index = torch::arange(10, torch::kLong).cpu();
    torch::Tensor result = at::index_put_(tensor, {index}, torch::ones({20}));

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 99 Column: 24 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

                  torch::Tensor index = torch::arange(10, torch::kLong).cpu();
    torch::Tensor result = at::index_put_(tensor, {index}, torch::ones({20}));
    torch::Tensor result_with_init_list = tensor.index_put_({index}, torch::ones({20}));
    ASSERT_TRUE(result.equal(result_with_init_list));
  }
  {
    torch::Tensor tensor = torch::randn({20, 20});
    torch::Tensor index = torch::arange(10, torch::kLong).cpu();
    torch::Tensor result = at::index_put_(tensor, {index}, torch::ones({1, 20}));

            

Reported by FlawFinder.

equal - Function does not check the second iterator for over-read conditions
Security

Line: 106 Column: 24 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

                  torch::Tensor index = torch::arange(10, torch::kLong).cpu();
    torch::Tensor result = at::index_put_(tensor, {index}, torch::ones({1, 20}));
    torch::Tensor result_with_init_list = tensor.index_put_({index}, torch::ones({1, 20}));
    ASSERT_TRUE(result.equal(result_with_init_list));
  }
}

TEST(TensorIndexingTest, TestSingleInt) {
  auto v = torch::randn({5, 7, 3});

            

Reported by FlawFinder.

caffe2/python/operator_test/concat_op_cost_test.py
4 issues
Missing module docstring
Error

Line: 1 Column: 1

              from collections import namedtuple

import numpy as np
from caffe2.python import core, workspace
from caffe2.python.test_util import TestCase


class TestConcatOpCost(TestCase):
    def test_columnwise_concat(self):

            

Reported by Pylint.

Missing class docstring
Error

Line: 8 Column: 1

              from caffe2.python.test_util import TestCase


class TestConcatOpCost(TestCase):
    def test_columnwise_concat(self):
        workspace.ResetWorkspace()
        workspace.FeedBlob("input_1", np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
        workspace.FeedBlob("input_2", np.array([[7], [8]], dtype=np.int32))
        concat_op = core.CreateOperator(

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 9 Column: 5

              

class TestConcatOpCost(TestCase):
    def test_columnwise_concat(self):
        workspace.ResetWorkspace()
        workspace.FeedBlob("input_1", np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
        workspace.FeedBlob("input_2", np.array([[7], [8]], dtype=np.int32))
        concat_op = core.CreateOperator(
            "Concat",

            

Reported by Pylint.

Missing function or method docstring
Error

Line: 38 Column: 5

                          sum(workspace.FetchBlob(b).nbytes for b in concat_op.output),
        )

    def test_split_then_concat(self):
        workspace.ResetWorkspace()
        workspace.FeedBlob("input", np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
        workspace.FeedBlob("split", np.array([1, 1, 1], dtype=np.int32))
        split_op = core.CreateOperator(
            "Split",

            

Reported by Pylint.

docs/source/scripts/build_activation_images.py
4 issues
Unable to import 'torch.nn.modules.activation'
Error

Line: 8 Column: 1

              """

import os.path
import torch.nn.modules.activation
import torch.autograd
import matplotlib

matplotlib.use('Agg')


            

Reported by Pylint.

Unable to import 'torch.autograd'
Error

Line: 9 Column: 1

              
import os.path
import torch.nn.modules.activation
import torch.autograd
import matplotlib

matplotlib.use('Agg')

import pylab

            

Reported by Pylint.

Redefining name 'function' from outer scope (line 70)
Error

Line: 53 Column: 19

              ]


def plot_function(function, **args):
    """
    Plot a function on the current plot. The additional arguments may
    be used to specify color, alpha, etc.
    """
    xrange = torch.arange(-7.0, 7.0, 0.01)  # We need to go beyond 6 for ReLU6

            

Reported by Pylint.

Import "import pylab" should be placed at the top of the module
Error

Line: 14 Column: 1

              
matplotlib.use('Agg')

import pylab


# Create a directory for the images, if it doesn't exist
ACTIVATION_IMAGE_PATH = os.path.join(
    os.path.realpath(os.path.join(__file__, "..")),

            

Reported by Pylint.