Coverage Report

Created: 2024-05-15 07:01

/src/bloaty/tests/strarr.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef BLOATY_TESTS_STRARR_H_
16
#define BLOATY_TESTS_STRARR_H_
17
18
#include <memory>
19
#include <string>
20
#include <vector>
21
22
// For constructing arrays of strings in the slightly peculiar format
23
// required by execve().
24
class StrArr {
25
 public:
26
  explicit StrArr(const std::vector<std::string>& strings)
27
0
      : size_(strings.size()), array_(new char*[size_ + 1]) {
28
0
    array_[size_] = NULL;
29
0
    for (size_t i = 0; i < strings.size(); i++) {
30
0
      // Can't use c_str() directly because array_ is not const char*.
31
0
      array_[i] = strdup(strings[i].c_str());
32
0
    }
33
0
  }
34
35
0
  ~StrArr() {
36
0
    // unique_ptr frees the array of pointers but not the pointed-to strings.
37
0
    for (int i = 0; i < size_; i++) {
38
0
      free(array_[i]);
39
0
    }
40
0
  }
41
42
0
  char **get() const { return array_.get(); }
43
44
0
  size_t size() const { return size_; }
45
46
 private:
47
  size_t size_;
48
  // Can't use vector<char*> because execve() takes ptr to non-const array.
49
  std::unique_ptr<char*[]> array_;
50
};
51
52
#endif // BLOATY_TESTS_STRARR_H_