1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/check_op.h"
6
7 #include <string.h>
8
9 #include <algorithm>
10 #include <cstdio>
11 #include <sstream>
12
13 #include "base/containers/span.h"
14 #include "base/logging.h"
15 #include "base/strings/cstring_view.h"
16
17 namespace logging {
18
CheckOpValueStr(int v)19 char* CheckOpValueStr(int v) {
20 char buf[50];
21 snprintf(buf, sizeof(buf), "%d", v);
22 return strdup(buf);
23 }
24
CheckOpValueStr(unsigned v)25 char* CheckOpValueStr(unsigned v) {
26 char buf[50];
27 snprintf(buf, sizeof(buf), "%u", v);
28 return strdup(buf);
29 }
30
CheckOpValueStr(long v)31 char* CheckOpValueStr(long v) {
32 char buf[50];
33 snprintf(buf, sizeof(buf), "%ld", v);
34 return strdup(buf);
35 }
36
CheckOpValueStr(unsigned long v)37 char* CheckOpValueStr(unsigned long v) {
38 char buf[50];
39 snprintf(buf, sizeof(buf), "%lu", v);
40 return strdup(buf);
41 }
42
CheckOpValueStr(long long v)43 char* CheckOpValueStr(long long v) {
44 char buf[50];
45 snprintf(buf, sizeof(buf), "%lld", v);
46 return strdup(buf);
47 }
48
CheckOpValueStr(unsigned long long v)49 char* CheckOpValueStr(unsigned long long v) {
50 char buf[50];
51 snprintf(buf, sizeof(buf), "%llu", v);
52 return strdup(buf);
53 }
54
CheckOpValueStr(const void * v)55 char* CheckOpValueStr(const void* v) {
56 char buf[50];
57 snprintf(buf, sizeof(buf), "%p", v);
58 return strdup(buf);
59 }
60
CheckOpValueStr(std::nullptr_t v)61 char* CheckOpValueStr(std::nullptr_t v) {
62 return strdup("nullptr");
63 }
64
CheckOpValueStr(const std::string & v)65 char* CheckOpValueStr(const std::string& v) {
66 return strdup(v.c_str());
67 }
68
CheckOpValueStr(std::string_view v)69 char* CheckOpValueStr(std::string_view v) {
70 // Ideally this would be `strndup`, but `strndup` is not portable. We have to
71 // use malloc() instead of HeapArray in order to match strdup() in the other
72 // overloads. The API contract is that the caller uses free() to release the
73 // pointer returned here.
74 char* ret = static_cast<char*>(malloc(v.size() + 1u));
75 auto [val, nul] =
76 // SAFETY: We allocated `ret` as `v.size() + 1` bytes above.
77 UNSAFE_BUFFERS(base::span<char>(ret, v.size() + 1u)).split_at(v.size());
78 val.copy_from(v);
79 nul.copy_from(base::span_from_ref('\0'));
80 return ret;
81 }
82
CheckOpValueStr(base::cstring_view v)83 char* CheckOpValueStr(base::cstring_view v) {
84 return strdup(v.c_str());
85 }
86
CheckOpValueStr(double v)87 char* CheckOpValueStr(double v) {
88 char buf[50];
89 snprintf(buf, sizeof(buf), "%.6lf", v);
90 return strdup(buf);
91 }
92
StreamValToStr(const void * v,void (* stream_func)(std::ostream &,const void *))93 char* StreamValToStr(const void* v,
94 void (*stream_func)(std::ostream&, const void*)) {
95 std::stringstream ss;
96 stream_func(ss, v);
97 return strdup(ss.str().c_str());
98 }
99
CreateCheckOpLogMessageString(const char * expr_str,char * v1_str,char * v2_str)100 char* CreateCheckOpLogMessageString(const char* expr_str,
101 char* v1_str,
102 char* v2_str) {
103 std::stringstream ss;
104 ss << "Check failed: " << expr_str << " (" << v1_str << " vs. " << v2_str
105 << ")";
106 free(v1_str);
107 free(v2_str);
108 return strdup(ss.str().c_str());
109 }
110
111 } // namespace logging
112