From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by sourceware.org (Postfix) with ESMTPS id 27BBA3857C7C for ; Thu, 4 Nov 2021 20:46:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 27BBA3857C7C Received: by mail-qt1-x82d.google.com with SMTP id 8so5372917qty.10 for ; Thu, 04 Nov 2021 13:46:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=67YM4zhlSqjqRbFj3XICJ06O+x4wjWGXR3f4lzw8rHo=; b=uolxNS3EXHsksLxUcRnvF7NLGmArov54ZytcroWepoo0RZHPfzgsBgujuOD+Rs8PLD qXbGdQXNZ5PGjIvaRYuSejneepC6LVluVO0Eook5oArABWXfS18Bqd4IbwSee8p2qTrk fiF02oYeE91fcj3kqSLkUnoK1i5bR25eT3+uc2ZfbvkJfaWAO8/5QDtSsG4jZ93xZdlI ZOm8R56MdPqNVFKRcqH+/EOrZxMuCXz8wlg5oB++NrhQfE/AMBLnxhkdEXgTt7OXz52P jEMUu0KeYqZCZ3NI5dVfirGCjRqQyFTtSt4oWTNXS9F8HYpcUsYzLzF76vaBKsnq9BYL jACA== X-Gm-Message-State: AOAM530TlnvSdik0BMNhnuGwwjOV0wtzIsWonzwOY8WexVYVZtKl3NQ7 olG0IfKGALHgk6IDxh2+yUnr88SasZ/rsw== X-Google-Smtp-Source: ABdhPJwtyHWk7N/LkMddRXo+xzDWtnBKX5hqi3BPl5DKneruMjulaqbn1s0aoazz9FR7VO7+3yXEWA== X-Received: by 2002:a05:622a:1450:: with SMTP id v16mr46200301qtx.25.1636058765490; Thu, 04 Nov 2021 13:46:05 -0700 (PDT) Received: from ?IPV6:2804:431:c7cb:b64f:4cb2:18f:d2ae:b202? ([2804:431:c7cb:b64f:4cb2:18f:d2ae:b202]) by smtp.gmail.com with ESMTPSA id c16sm4661417qte.95.2021.11.04.13.46.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Nov 2021 13:46:05 -0700 (PDT) Message-ID: <98556e3e-2869-64f0-574e-7a64503185c2@linaro.org> Date: Thu, 4 Nov 2021 17:46:03 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: glibc strerrorname_np Content-Language: en-US To: libc-alpha@sourceware.org, Jonny Grant References: From: Adhemerval Zanella In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Nov 2021 20:46:09 -0000 On 04/11/2021 17:23, Jonny Grant wrote: > Hi Carlos > I was pleased to see you added strerrorname_np() > > May I ask, I couldn't find the file your implementation is in - could you point it out to me in the glibc repository please? It is on string/strerrorname_np.c, which calls __get_errname() defined at stdio-common/errlist.c. > > I noticed on the man page it may return NULL, which is a shame, as then it means we always need to check that before using in every printf etc :- > > printf("err %s\n", strerrorname_np(myerr)?strerrorname_np(myerr), "Unknown err"); I didn't considered printf() when I added strerrorname_np(). Maybe an empty string ("") would be better than NULL. >   > I'd done my own version a while ago as strerrno_s(), and assumed I could never get it accepted anywhere like glibc. > Probably I should have tried to submit it to glibc! > https://github.com/jonnygrant/safec/blob/master/strerrno.c > > Would something like my implementation ever be accepted? > errno_t strerrno_s(char * const buf, const rsize_t buflen, const errno_t errnum) So this is basically: int strerrno_s (char *buf, size_t buflen, int errnum) { const char *r = strerrorname_np (errnum); __snprintf (buf, buflen, "%s", r == NULL ? "" : r); return errnum; } I don't see much gain on adding another wrapper to format errno, the idea of strerrorname_np() was to provide a async-signal-safe way to map errno to string (by avoiding translation). > > > Last quick question, do you know why strerror_r() is considered safer than strerror()? I guess someone could trash the memory returned by it? > > char * errstr = strerror(EINVAL); > errstr[0] = '\0';  // trashed the process copy of the string. (or even SEGV it, if it was in some static section of the ELF?) It is undefined-behavior when you modify the return value (both C and POSIX are explicit about it). On glibc, both strerror() and strerror_l() returns the same thread local buffer (so you might modify in a thread-safe manner).