From patchwork Sat Mar 27 18:09:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 42790 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8F9163857821; Sat, 27 Mar 2021 18:10:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8F9163857821 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616868611; bh=81YYrpm/05H/mbtL+VPxbNiPr+zdUCKnEYY1zSgRGzc=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=BbHob9Nab/+DGhI6wnXQe56wp3/EonDFiIqko52K86lrKOKBatxSmVRZvOUsr2u8T t70ME0GIk5KYYMNpRKGG6JO6O8YfMgCQnNdjBPQQpqMHuapJ5cFCR+B+oxCahWSKq8 jwv0Ct2ICvcMLk3Zt87lZ2K6KaJw9z/ZAc/WAsHw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 20D5E385801A for ; Sat, 27 Mar 2021 18:10:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 20D5E385801A Received: by mail-ot1-x335.google.com with SMTP id t23-20020a0568301e37b02901b65ab30024so8364798otr.4 for ; Sat, 27 Mar 2021 11:10:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=81YYrpm/05H/mbtL+VPxbNiPr+zdUCKnEYY1zSgRGzc=; b=l3DxGzqYyAA8Y3EeUH6wA2Vx7UiDeKIKEG6qz5iW0y4wmoCIXTyL/oSe8znaUPm39A YUJHNCGa2lbEc+thhLrjUnaZicKODOAK0JpHg2FDrbU3KXD3A/QUGzOuiUO5+DlP53H3 IyYGIESGZTAsOSMHhrmsWw0fGpoiL0OxY2KUjmP65HpPhQ5If8950ZytKG2zRou05VAv CxM5970sGLQIFIpqwXdGsH8791CRDXKKri3XNScNbE0eUDQUkJEQNHm2aPHqc2KcUV/k qZurxIHSODMoYl6Pa5zqhUmDp9G2vUVomU1r8Eab27ordGDsYCXcZjdfoznjAuGEVfQv 33GQ== X-Gm-Message-State: AOAM532m6Pt0324ERcK49Y7272dmyzrZH/cGRdkH2Y7f1xMbWjSS7kxV Otbx76LVNurAP9cfFqiUko7/9HZ7Inc117QE1Io98h/FdkxPrg== X-Google-Smtp-Source: ABdhPJwNceRCG5YRJjW7Av283cMVOyU5TX+Ceo1LbKqYjIPszActWE1r+pEgzMGhj0jXCKr35nRx4riMw9QMM518ruw= X-Received: by 2002:a05:6830:1515:: with SMTP id k21mr16692924otp.269.1616868608475; Sat, 27 Mar 2021 11:10:08 -0700 (PDT) MIME-Version: 1.0 References: <20210327162449.3215538-1-hjl.tools@gmail.com> <87ft0g8oz4.fsf@mid.deneb.enyo.de> In-Reply-To: <87ft0g8oz4.fsf@mid.deneb.enyo.de> Date: Sat, 27 Mar 2021 11:09:32 -0700 Message-ID: Subject: [PATCH v2] test-strnlen.c: Check that strnlen won't go beyond the maximum length To: Florian Weimer X-Spam-Status: No, score=-3035.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: "H.J. Lu via Libc-alpha" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Sat, Mar 27, 2021 at 10:51 AM Florian Weimer wrote: > > * H. J. Lu via Libc-alpha: > > > Run strnlen tests on strings without the null byte around the page > > boundary. > > --- > > string/test-strnlen.c | 26 ++++++++++++++++++++++++++ > > 1 file changed, 26 insertions(+) > > > > diff --git a/string/test-strnlen.c b/string/test-strnlen.c > > index 61eb521dc1..a49d93afa2 100644 > > --- a/string/test-strnlen.c > > +++ b/string/test-strnlen.c > > @@ -196,6 +196,31 @@ do_page_tests (void) > > } > > } > > > > +/* Tests meant to unveil fail on implementation that does not access bytes > > + without the null character around the page boundary accordingly. */ > > I don't understand the comment. Based on it, I would have expected a > null byte at the page boundary, which is incorrectly ignored by the > implementation. It places strings ending at page boundary without the null byte. If an implementation goes beyond EXP_LEN, it will trigger the segfault. > > +static void > > +do_page_2_tests (void) > > +{ > > + size_t i, exp_len, offset; > > + size_t last_offset = page_size / sizeof (CHAR); > > + > > + CHAR *s = (CHAR *) buf2; > > + memset (s, 65, page_size); > > I think this needs to use wmemset for the WIDE case, see > wcsmbs/test-wcsnlen.c. Fixed. Here is the v2 patch. From c5d32deca0a024784fa3d9743ddffe38b567ec8c Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Sat, 27 Mar 2021 09:06:39 -0700 Subject: [PATCH v2] test-strnlen.c: Check that strnlen won't go beyond the maximum length Place strings ending at page boundary without the null byte. If an implementation goes beyond EXP_LEN, it will trigger the segfault. --- string/test-strnlen.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/string/test-strnlen.c b/string/test-strnlen.c index 0293acbc71..d70faa26ab 100644 --- a/string/test-strnlen.c +++ b/string/test-strnlen.c @@ -198,6 +198,35 @@ do_page_tests (void) } } +/* Tests meant to unveil fail on implementations that access bytes + beyond the maxium length. */ + +static void +do_page_2_tests (void) +{ + size_t i, exp_len, offset; + size_t last_offset = page_size / sizeof (CHAR); + + CHAR *s = (CHAR *) buf2; + MEMSET (s, 65, last_offset); + + /* Place short strings ending at page boundary without the null + byte. */ + offset = last_offset; + for (i = 0; i < 128; i++) + { + /* Decrease offset to stress several sizes and alignments. */ + offset--; + exp_len = last_offset - offset; + FOR_EACH_IMPL (impl, 0) + { + /* If an implementation goes beyond EXP_LEN, it will trigger + the segfault. */ + do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len); + } + } +} + int test_main (void) { @@ -244,6 +273,7 @@ test_main (void) do_random_tests (); do_page_tests (); + do_page_2_tests (); return ret; } -- 2.30.2