18dffb485Schristos /* Safe version of strerror for GDB, the GNU debugger. 28dffb485Schristos 3*5ba1f45fSchristos Copyright (C) 2006-2024 Free Software Foundation, Inc. 48dffb485Schristos 58dffb485Schristos This file is part of GDB. 68dffb485Schristos 78dffb485Schristos This program is free software; you can redistribute it and/or modify 88dffb485Schristos it under the terms of the GNU General Public License as published by 98dffb485Schristos the Free Software Foundation; either version 3 of the License, or 108dffb485Schristos (at your option) any later version. 118dffb485Schristos 128dffb485Schristos This program is distributed in the hope that it will be useful, 138dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 148dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158dffb485Schristos GNU General Public License for more details. 168dffb485Schristos 178dffb485Schristos You should have received a copy of the GNU General Public License 188dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #include <string.h> 218dffb485Schristos 228dffb485Schristos /* There are two different versions of strerror_r; one is GNU-specific, the 238dffb485Schristos other XSI-compliant. They differ in the return type. This overload lets 248dffb485Schristos us choose the right behavior for each return type. We cannot rely on Gnulib 258dffb485Schristos to solve this for us because IPA does not use Gnulib but uses this 268dffb485Schristos function. */ 278dffb485Schristos 288dffb485Schristos /* Called if we have a XSI-compliant strerror_r. */ 298dffb485Schristos ATTRIBUTE_UNUSED static char * 308dffb485Schristos select_strerror_r (int res, char *buf) 318dffb485Schristos { 328dffb485Schristos return res == 0 ? buf : nullptr; 338dffb485Schristos } 348dffb485Schristos 358dffb485Schristos /* Called if we have a GNU strerror_r. */ 368dffb485Schristos ATTRIBUTE_UNUSED static char * 378dffb485Schristos select_strerror_r (char *res, char *) 388dffb485Schristos { 398dffb485Schristos return res; 408dffb485Schristos } 418dffb485Schristos 428dffb485Schristos /* Implementation of safe_strerror as defined in common-utils.h. */ 438dffb485Schristos 448dffb485Schristos const char * 458dffb485Schristos safe_strerror (int errnum) 468dffb485Schristos { 478dffb485Schristos static thread_local char buf[1024]; 488dffb485Schristos 498dffb485Schristos char *res = select_strerror_r (strerror_r (errnum, buf, sizeof (buf)), buf); 508dffb485Schristos if (res != nullptr) 518dffb485Schristos return res; 528dffb485Schristos 538dffb485Schristos xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); 548dffb485Schristos return buf; 558dffb485Schristos } 56