• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Abseil Authors.
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 //      https://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 #include "absl/status/status.h"
15 
16 #include <errno.h>
17 
18 #include <cassert>
19 #include <utility>
20 
21 #include "absl/base/internal/raw_logging.h"
22 #include "absl/base/internal/strerror.h"
23 #include "absl/base/macros.h"
24 #include "absl/debugging/stacktrace.h"
25 #include "absl/debugging/symbolize.h"
26 #include "absl/status/status_payload_printer.h"
27 #include "absl/strings/escaping.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_split.h"
31 
32 namespace absl {
33 ABSL_NAMESPACE_BEGIN
34 
StatusCodeToString(StatusCode code)35 std::string StatusCodeToString(StatusCode code) {
36   switch (code) {
37     case StatusCode::kOk:
38       return "OK";
39     case StatusCode::kCancelled:
40       return "CANCELLED";
41     case StatusCode::kUnknown:
42       return "UNKNOWN";
43     case StatusCode::kInvalidArgument:
44       return "INVALID_ARGUMENT";
45     case StatusCode::kDeadlineExceeded:
46       return "DEADLINE_EXCEEDED";
47     case StatusCode::kNotFound:
48       return "NOT_FOUND";
49     case StatusCode::kAlreadyExists:
50       return "ALREADY_EXISTS";
51     case StatusCode::kPermissionDenied:
52       return "PERMISSION_DENIED";
53     case StatusCode::kUnauthenticated:
54       return "UNAUTHENTICATED";
55     case StatusCode::kResourceExhausted:
56       return "RESOURCE_EXHAUSTED";
57     case StatusCode::kFailedPrecondition:
58       return "FAILED_PRECONDITION";
59     case StatusCode::kAborted:
60       return "ABORTED";
61     case StatusCode::kOutOfRange:
62       return "OUT_OF_RANGE";
63     case StatusCode::kUnimplemented:
64       return "UNIMPLEMENTED";
65     case StatusCode::kInternal:
66       return "INTERNAL";
67     case StatusCode::kUnavailable:
68       return "UNAVAILABLE";
69     case StatusCode::kDataLoss:
70       return "DATA_LOSS";
71     default:
72       return "";
73   }
74 }
75 
operator <<(std::ostream & os,StatusCode code)76 std::ostream& operator<<(std::ostream& os, StatusCode code) {
77   return os << StatusCodeToString(code);
78 }
79 
80 namespace status_internal {
81 
FindPayloadIndexByUrl(const Payloads * payloads,absl::string_view type_url)82 static absl::optional<size_t> FindPayloadIndexByUrl(
83     const Payloads* payloads, absl::string_view type_url) {
84   if (payloads == nullptr) return absl::nullopt;
85 
86   for (size_t i = 0; i < payloads->size(); ++i) {
87     if ((*payloads)[i].type_url == type_url) return i;
88   }
89 
90   return absl::nullopt;
91 }
92 
93 // Convert canonical code to a value known to this binary.
MapToLocalCode(int value)94 absl::StatusCode MapToLocalCode(int value) {
95   absl::StatusCode code = static_cast<absl::StatusCode>(value);
96   switch (code) {
97     case absl::StatusCode::kOk:
98     case absl::StatusCode::kCancelled:
99     case absl::StatusCode::kUnknown:
100     case absl::StatusCode::kInvalidArgument:
101     case absl::StatusCode::kDeadlineExceeded:
102     case absl::StatusCode::kNotFound:
103     case absl::StatusCode::kAlreadyExists:
104     case absl::StatusCode::kPermissionDenied:
105     case absl::StatusCode::kResourceExhausted:
106     case absl::StatusCode::kFailedPrecondition:
107     case absl::StatusCode::kAborted:
108     case absl::StatusCode::kOutOfRange:
109     case absl::StatusCode::kUnimplemented:
110     case absl::StatusCode::kInternal:
111     case absl::StatusCode::kUnavailable:
112     case absl::StatusCode::kDataLoss:
113     case absl::StatusCode::kUnauthenticated:
114       return code;
115     default:
116       return absl::StatusCode::kUnknown;
117   }
118 }
119 }  // namespace status_internal
120 
GetPayload(absl::string_view type_url) const121 absl::optional<absl::Cord> Status::GetPayload(
122     absl::string_view type_url) const {
123   const auto* payloads = GetPayloads();
124   absl::optional<size_t> index =
125       status_internal::FindPayloadIndexByUrl(payloads, type_url);
126   if (index.has_value()) return (*payloads)[index.value()].payload;
127 
128   return absl::nullopt;
129 }
130 
SetPayload(absl::string_view type_url,absl::Cord payload)131 void Status::SetPayload(absl::string_view type_url, absl::Cord payload) {
132   if (ok()) return;
133 
134   PrepareToModify();
135 
136   status_internal::StatusRep* rep = RepToPointer(rep_);
137   if (!rep->payloads) {
138     rep->payloads = absl::make_unique<status_internal::Payloads>();
139   }
140 
141   absl::optional<size_t> index =
142       status_internal::FindPayloadIndexByUrl(rep->payloads.get(), type_url);
143   if (index.has_value()) {
144     (*rep->payloads)[index.value()].payload = std::move(payload);
145     return;
146   }
147 
148   rep->payloads->push_back({std::string(type_url), std::move(payload)});
149 }
150 
ErasePayload(absl::string_view type_url)151 bool Status::ErasePayload(absl::string_view type_url) {
152   absl::optional<size_t> index =
153       status_internal::FindPayloadIndexByUrl(GetPayloads(), type_url);
154   if (index.has_value()) {
155     PrepareToModify();
156     GetPayloads()->erase(GetPayloads()->begin() + index.value());
157     if (GetPayloads()->empty() && message().empty()) {
158       // Special case: If this can be represented inlined, it MUST be
159       // inlined (EqualsSlow depends on this behavior).
160       StatusCode c = static_cast<StatusCode>(raw_code());
161       Unref(rep_);
162       rep_ = CodeToInlinedRep(c);
163     }
164     return true;
165   }
166 
167   return false;
168 }
169 
ForEachPayload(absl::FunctionRef<void (absl::string_view,const absl::Cord &)> visitor) const170 void Status::ForEachPayload(
171     absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
172     const {
173   if (auto* payloads = GetPayloads()) {
174     bool in_reverse =
175         payloads->size() > 1 && reinterpret_cast<uintptr_t>(payloads) % 13 > 6;
176 
177     for (size_t index = 0; index < payloads->size(); ++index) {
178       const auto& elem =
179           (*payloads)[in_reverse ? payloads->size() - 1 - index : index];
180 
181 #ifdef NDEBUG
182       visitor(elem.type_url, elem.payload);
183 #else
184       // In debug mode invalidate the type url to prevent users from relying on
185       // this string lifetime.
186 
187       // NOLINTNEXTLINE intentional extra conversion to force temporary.
188       visitor(std::string(elem.type_url), elem.payload);
189 #endif  // NDEBUG
190     }
191   }
192 }
193 
EmptyString()194 const std::string* Status::EmptyString() {
195   static union EmptyString {
196     std::string str;
197     ~EmptyString() {}
198   } empty = {{}};
199   return &empty.str;
200 }
201 
202 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
203 constexpr const char Status::kMovedFromString[];
204 #endif
205 
MovedFromString()206 const std::string* Status::MovedFromString() {
207   static std::string* moved_from_string = new std::string(kMovedFromString);
208   return moved_from_string;
209 }
210 
UnrefNonInlined(uintptr_t rep)211 void Status::UnrefNonInlined(uintptr_t rep) {
212   status_internal::StatusRep* r = RepToPointer(rep);
213   // Fast path: if ref==1, there is no need for a RefCountDec (since
214   // this is the only reference and therefore no other thread is
215   // allowed to be mucking with r).
216   if (r->ref.load(std::memory_order_acquire) == 1 ||
217       r->ref.fetch_sub(1, std::memory_order_acq_rel) - 1 == 0) {
218     delete r;
219   }
220 }
221 
Status(absl::StatusCode code,absl::string_view msg)222 Status::Status(absl::StatusCode code, absl::string_view msg)
223     : rep_(CodeToInlinedRep(code)) {
224   if (code != absl::StatusCode::kOk && !msg.empty()) {
225     rep_ = PointerToRep(new status_internal::StatusRep(code, msg, nullptr));
226   }
227 }
228 
raw_code() const229 int Status::raw_code() const {
230   if (IsInlined(rep_)) {
231     return static_cast<int>(InlinedRepToCode(rep_));
232   }
233   status_internal::StatusRep* rep = RepToPointer(rep_);
234   return static_cast<int>(rep->code);
235 }
236 
code() const237 absl::StatusCode Status::code() const {
238   return status_internal::MapToLocalCode(raw_code());
239 }
240 
PrepareToModify()241 void Status::PrepareToModify() {
242   ABSL_RAW_CHECK(!ok(), "PrepareToModify shouldn't be called on OK status.");
243   if (IsInlined(rep_)) {
244     rep_ = PointerToRep(new status_internal::StatusRep(
245         static_cast<absl::StatusCode>(raw_code()), absl::string_view(),
246         nullptr));
247     return;
248   }
249 
250   uintptr_t rep_i = rep_;
251   status_internal::StatusRep* rep = RepToPointer(rep_);
252   if (rep->ref.load(std::memory_order_acquire) != 1) {
253     std::unique_ptr<status_internal::Payloads> payloads;
254     if (rep->payloads) {
255       payloads = absl::make_unique<status_internal::Payloads>(*rep->payloads);
256     }
257     status_internal::StatusRep* const new_rep = new status_internal::StatusRep(
258         rep->code, message(), std::move(payloads));
259     rep_ = PointerToRep(new_rep);
260     UnrefNonInlined(rep_i);
261   }
262 }
263 
EqualsSlow(const absl::Status & a,const absl::Status & b)264 bool Status::EqualsSlow(const absl::Status& a, const absl::Status& b) {
265   if (IsInlined(a.rep_) != IsInlined(b.rep_)) return false;
266   if (a.message() != b.message()) return false;
267   if (a.raw_code() != b.raw_code()) return false;
268   if (a.GetPayloads() == b.GetPayloads()) return true;
269 
270   const status_internal::Payloads no_payloads;
271   const status_internal::Payloads* larger_payloads =
272       a.GetPayloads() ? a.GetPayloads() : &no_payloads;
273   const status_internal::Payloads* smaller_payloads =
274       b.GetPayloads() ? b.GetPayloads() : &no_payloads;
275   if (larger_payloads->size() < smaller_payloads->size()) {
276     std::swap(larger_payloads, smaller_payloads);
277   }
278   if ((larger_payloads->size() - smaller_payloads->size()) > 1) return false;
279   // Payloads can be ordered differently, so we can't just compare payload
280   // vectors.
281   for (const auto& payload : *larger_payloads) {
282 
283     bool found = false;
284     for (const auto& other_payload : *smaller_payloads) {
285       if (payload.type_url == other_payload.type_url) {
286         if (payload.payload != other_payload.payload) {
287           return false;
288         }
289         found = true;
290         break;
291       }
292     }
293     if (!found) return false;
294   }
295   return true;
296 }
297 
ToStringSlow(StatusToStringMode mode) const298 std::string Status::ToStringSlow(StatusToStringMode mode) const {
299   std::string text;
300   absl::StrAppend(&text, absl::StatusCodeToString(code()), ": ", message());
301 
302   const bool with_payload = (mode & StatusToStringMode::kWithPayload) ==
303                             StatusToStringMode::kWithPayload;
304 
305   if (with_payload) {
306     status_internal::StatusPayloadPrinter printer =
307         status_internal::GetStatusPayloadPrinter();
308     this->ForEachPayload([&](absl::string_view type_url,
309                              const absl::Cord& payload) {
310       absl::optional<std::string> result;
311       if (printer) result = printer(type_url, payload);
312       absl::StrAppend(
313           &text, " [", type_url, "='",
314           result.has_value() ? *result : absl::CHexEscape(std::string(payload)),
315           "']");
316     });
317   }
318 
319   return text;
320 }
321 
operator <<(std::ostream & os,const Status & x)322 std::ostream& operator<<(std::ostream& os, const Status& x) {
323   os << x.ToString(StatusToStringMode::kWithEverything);
324   return os;
325 }
326 
AbortedError(absl::string_view message)327 Status AbortedError(absl::string_view message) {
328   return Status(absl::StatusCode::kAborted, message);
329 }
330 
AlreadyExistsError(absl::string_view message)331 Status AlreadyExistsError(absl::string_view message) {
332   return Status(absl::StatusCode::kAlreadyExists, message);
333 }
334 
CancelledError(absl::string_view message)335 Status CancelledError(absl::string_view message) {
336   return Status(absl::StatusCode::kCancelled, message);
337 }
338 
DataLossError(absl::string_view message)339 Status DataLossError(absl::string_view message) {
340   return Status(absl::StatusCode::kDataLoss, message);
341 }
342 
DeadlineExceededError(absl::string_view message)343 Status DeadlineExceededError(absl::string_view message) {
344   return Status(absl::StatusCode::kDeadlineExceeded, message);
345 }
346 
FailedPreconditionError(absl::string_view message)347 Status FailedPreconditionError(absl::string_view message) {
348   return Status(absl::StatusCode::kFailedPrecondition, message);
349 }
350 
InternalError(absl::string_view message)351 Status InternalError(absl::string_view message) {
352   return Status(absl::StatusCode::kInternal, message);
353 }
354 
InvalidArgumentError(absl::string_view message)355 Status InvalidArgumentError(absl::string_view message) {
356   return Status(absl::StatusCode::kInvalidArgument, message);
357 }
358 
NotFoundError(absl::string_view message)359 Status NotFoundError(absl::string_view message) {
360   return Status(absl::StatusCode::kNotFound, message);
361 }
362 
OutOfRangeError(absl::string_view message)363 Status OutOfRangeError(absl::string_view message) {
364   return Status(absl::StatusCode::kOutOfRange, message);
365 }
366 
PermissionDeniedError(absl::string_view message)367 Status PermissionDeniedError(absl::string_view message) {
368   return Status(absl::StatusCode::kPermissionDenied, message);
369 }
370 
ResourceExhaustedError(absl::string_view message)371 Status ResourceExhaustedError(absl::string_view message) {
372   return Status(absl::StatusCode::kResourceExhausted, message);
373 }
374 
UnauthenticatedError(absl::string_view message)375 Status UnauthenticatedError(absl::string_view message) {
376   return Status(absl::StatusCode::kUnauthenticated, message);
377 }
378 
UnavailableError(absl::string_view message)379 Status UnavailableError(absl::string_view message) {
380   return Status(absl::StatusCode::kUnavailable, message);
381 }
382 
UnimplementedError(absl::string_view message)383 Status UnimplementedError(absl::string_view message) {
384   return Status(absl::StatusCode::kUnimplemented, message);
385 }
386 
UnknownError(absl::string_view message)387 Status UnknownError(absl::string_view message) {
388   return Status(absl::StatusCode::kUnknown, message);
389 }
390 
IsAborted(const Status & status)391 bool IsAborted(const Status& status) {
392   return status.code() == absl::StatusCode::kAborted;
393 }
394 
IsAlreadyExists(const Status & status)395 bool IsAlreadyExists(const Status& status) {
396   return status.code() == absl::StatusCode::kAlreadyExists;
397 }
398 
IsCancelled(const Status & status)399 bool IsCancelled(const Status& status) {
400   return status.code() == absl::StatusCode::kCancelled;
401 }
402 
IsDataLoss(const Status & status)403 bool IsDataLoss(const Status& status) {
404   return status.code() == absl::StatusCode::kDataLoss;
405 }
406 
IsDeadlineExceeded(const Status & status)407 bool IsDeadlineExceeded(const Status& status) {
408   return status.code() == absl::StatusCode::kDeadlineExceeded;
409 }
410 
IsFailedPrecondition(const Status & status)411 bool IsFailedPrecondition(const Status& status) {
412   return status.code() == absl::StatusCode::kFailedPrecondition;
413 }
414 
IsInternal(const Status & status)415 bool IsInternal(const Status& status) {
416   return status.code() == absl::StatusCode::kInternal;
417 }
418 
IsInvalidArgument(const Status & status)419 bool IsInvalidArgument(const Status& status) {
420   return status.code() == absl::StatusCode::kInvalidArgument;
421 }
422 
IsNotFound(const Status & status)423 bool IsNotFound(const Status& status) {
424   return status.code() == absl::StatusCode::kNotFound;
425 }
426 
IsOutOfRange(const Status & status)427 bool IsOutOfRange(const Status& status) {
428   return status.code() == absl::StatusCode::kOutOfRange;
429 }
430 
IsPermissionDenied(const Status & status)431 bool IsPermissionDenied(const Status& status) {
432   return status.code() == absl::StatusCode::kPermissionDenied;
433 }
434 
IsResourceExhausted(const Status & status)435 bool IsResourceExhausted(const Status& status) {
436   return status.code() == absl::StatusCode::kResourceExhausted;
437 }
438 
IsUnauthenticated(const Status & status)439 bool IsUnauthenticated(const Status& status) {
440   return status.code() == absl::StatusCode::kUnauthenticated;
441 }
442 
IsUnavailable(const Status & status)443 bool IsUnavailable(const Status& status) {
444   return status.code() == absl::StatusCode::kUnavailable;
445 }
446 
IsUnimplemented(const Status & status)447 bool IsUnimplemented(const Status& status) {
448   return status.code() == absl::StatusCode::kUnimplemented;
449 }
450 
IsUnknown(const Status & status)451 bool IsUnknown(const Status& status) {
452   return status.code() == absl::StatusCode::kUnknown;
453 }
454 
ErrnoToStatusCode(int error_number)455 StatusCode ErrnoToStatusCode(int error_number) {
456   switch (error_number) {
457     case 0:
458       return StatusCode::kOk;
459     case EINVAL:        // Invalid argument
460     case ENAMETOOLONG:  // Filename too long
461     case E2BIG:         // Argument list too long
462     case EDESTADDRREQ:  // Destination address required
463     case EDOM:          // Mathematics argument out of domain of function
464     case EFAULT:        // Bad address
465     case EILSEQ:        // Illegal byte sequence
466     case ENOPROTOOPT:   // Protocol not available
467     case ENOSTR:        // Not a STREAM
468     case ENOTSOCK:      // Not a socket
469     case ENOTTY:        // Inappropriate I/O control operation
470     case EPROTOTYPE:    // Protocol wrong type for socket
471     case ESPIPE:        // Invalid seek
472       return StatusCode::kInvalidArgument;
473     case ETIMEDOUT:  // Connection timed out
474     case ETIME:      // Timer expired
475       return StatusCode::kDeadlineExceeded;
476     case ENODEV:  // No such device
477     case ENOENT:  // No such file or directory
478 #ifdef ENOMEDIUM
479     case ENOMEDIUM:  // No medium found
480 #endif
481     case ENXIO:  // No such device or address
482     case ESRCH:  // No such process
483       return StatusCode::kNotFound;
484     case EEXIST:         // File exists
485     case EADDRNOTAVAIL:  // Address not available
486     case EALREADY:       // Connection already in progress
487 #ifdef ENOTUNIQ
488     case ENOTUNIQ:  // Name not unique on network
489 #endif
490       return StatusCode::kAlreadyExists;
491     case EPERM:   // Operation not permitted
492     case EACCES:  // Permission denied
493 #ifdef ENOKEY
494     case ENOKEY:  // Required key not available
495 #endif
496     case EROFS:  // Read only file system
497       return StatusCode::kPermissionDenied;
498     case ENOTEMPTY:   // Directory not empty
499     case EISDIR:      // Is a directory
500     case ENOTDIR:     // Not a directory
501     case EADDRINUSE:  // Address already in use
502     case EBADF:       // Invalid file descriptor
503 #ifdef EBADFD
504     case EBADFD:  // File descriptor in bad state
505 #endif
506     case EBUSY:    // Device or resource busy
507     case ECHILD:   // No child processes
508     case EISCONN:  // Socket is connected
509 #ifdef EISNAM
510     case EISNAM:  // Is a named type file
511 #endif
512 #ifdef ENOTBLK
513     case ENOTBLK:  // Block device required
514 #endif
515     case ENOTCONN:  // The socket is not connected
516     case EPIPE:     // Broken pipe
517 #ifdef ESHUTDOWN
518     case ESHUTDOWN:  // Cannot send after transport endpoint shutdown
519 #endif
520     case ETXTBSY:  // Text file busy
521 #ifdef EUNATCH
522     case EUNATCH:  // Protocol driver not attached
523 #endif
524       return StatusCode::kFailedPrecondition;
525     case ENOSPC:  // No space left on device
526 #ifdef EDQUOT
527     case EDQUOT:  // Disk quota exceeded
528 #endif
529     case EMFILE:   // Too many open files
530     case EMLINK:   // Too many links
531     case ENFILE:   // Too many open files in system
532     case ENOBUFS:  // No buffer space available
533     case ENODATA:  // No message is available on the STREAM read queue
534     case ENOMEM:   // Not enough space
535     case ENOSR:    // No STREAM resources
536 #ifdef EUSERS
537     case EUSERS:  // Too many users
538 #endif
539       return StatusCode::kResourceExhausted;
540 #ifdef ECHRNG
541     case ECHRNG:  // Channel number out of range
542 #endif
543     case EFBIG:      // File too large
544     case EOVERFLOW:  // Value too large to be stored in data type
545     case ERANGE:     // Result too large
546       return StatusCode::kOutOfRange;
547 #ifdef ENOPKG
548     case ENOPKG:  // Package not installed
549 #endif
550     case ENOSYS:        // Function not implemented
551     case ENOTSUP:       // Operation not supported
552     case EAFNOSUPPORT:  // Address family not supported
553 #ifdef EPFNOSUPPORT
554     case EPFNOSUPPORT:  // Protocol family not supported
555 #endif
556     case EPROTONOSUPPORT:  // Protocol not supported
557 #ifdef ESOCKTNOSUPPORT
558     case ESOCKTNOSUPPORT:  // Socket type not supported
559 #endif
560     case EXDEV:  // Improper link
561       return StatusCode::kUnimplemented;
562     case EAGAIN:  // Resource temporarily unavailable
563 #ifdef ECOMM
564     case ECOMM:  // Communication error on send
565 #endif
566     case ECONNREFUSED:  // Connection refused
567     case ECONNABORTED:  // Connection aborted
568     case ECONNRESET:    // Connection reset
569     case EINTR:         // Interrupted function call
570 #ifdef EHOSTDOWN
571     case EHOSTDOWN:  // Host is down
572 #endif
573     case EHOSTUNREACH:  // Host is unreachable
574     case ENETDOWN:      // Network is down
575     case ENETRESET:     // Connection aborted by network
576     case ENETUNREACH:   // Network unreachable
577     case ENOLCK:        // No locks available
578     case ENOLINK:       // Link has been severed
579 #ifdef ENONET
580     case ENONET:  // Machine is not on the network
581 #endif
582       return StatusCode::kUnavailable;
583     case EDEADLK:  // Resource deadlock avoided
584 #ifdef ESTALE
585     case ESTALE:  // Stale file handle
586 #endif
587       return StatusCode::kAborted;
588     case ECANCELED:  // Operation cancelled
589       return StatusCode::kCancelled;
590     default:
591       return StatusCode::kUnknown;
592   }
593 }
594 
595 namespace {
MessageForErrnoToStatus(int error_number,absl::string_view message)596 std::string MessageForErrnoToStatus(int error_number,
597                                     absl::string_view message) {
598   return absl::StrCat(message, ": ",
599                       absl::base_internal::StrError(error_number));
600 }
601 }  // namespace
602 
ErrnoToStatus(int error_number,absl::string_view message)603 Status ErrnoToStatus(int error_number, absl::string_view message) {
604   return Status(ErrnoToStatusCode(error_number),
605                 MessageForErrnoToStatus(error_number, message));
606 }
607 
608 namespace status_internal {
609 
MakeCheckFailString(const absl::Status * status,const char * prefix)610 std::string* MakeCheckFailString(const absl::Status* status,
611                                  const char* prefix) {
612   return new std::string(
613       absl::StrCat(prefix, " (",
614                    status->ToString(StatusToStringMode::kWithEverything), ")"));
615 }
616 
617 }  // namespace status_internal
618 
StatusMessageAsCStr(const Status & status)619 const char* StatusMessageAsCStr(const Status& status) {
620   // As an internal implementation detail, we guarantee that if status.message()
621   // is non-empty, then the resulting string_view is null terminated.
622   auto sv_message = status.message();
623   return sv_message.empty() ? "" : sv_message.data();
624 }
625 
626 ABSL_NAMESPACE_END
627 }  // namespace absl
628