From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by sourceware.org (Postfix) with ESMTPS id 7C88B3858D1E for ; Fri, 2 Jun 2023 05:22:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C88B3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=asu.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=asu.edu Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-568a1011488so17277577b3.0 for ; Thu, 01 Jun 2023 22:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=asu-edu.20221208.gappssmtp.com; s=20221208; t=1685683361; x=1688275361; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=z7XHfqkzYMoZB2skx4eDigYZcprHw9KiLbvEJ1+BAc8=; b=2AXv6SzaYuP6FEVA3/wFYCEqV+5Kyvzx7ie26xneSf/kgnseXd7dBHdK5eeoc8cNBE 0MHwaLkmHU42A/+ItLwDS8zndPl19PDdot7ajNGr3rM7y7KwQqCmSUDXi9o+NRxNjwS4 zlRqbgyBlJrdhOcBJMH48bsBbzsYZkWpZDo7ERENP8YXx+b2yRpHwQDcoHzlB1Gwof/d H6CpVHAX5XzWNyeJfkbEP/MS1Lf/KfmBcfTQ5gS7HphQm/JHNIfXpLmlpOk6pP9xhmml Qjzl3t5izIYc4EeKG9zzlmb4vJL4zi49aQhHY2ssvb+YqUgV2yUzfpSiHSUwaNjxRyhK t0mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685683361; x=1688275361; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=z7XHfqkzYMoZB2skx4eDigYZcprHw9KiLbvEJ1+BAc8=; b=RvZe9GSCt/mrnD0xp6lYqMrUEkFGgUyZXpUeYwyiZwMigsgwtJqFF/f//cRSnoNXmB YKT8CzBuuMU8Bj6lT4D5gotM88LxSbQ84QunjO4lc3BbTT5DJ9kHiNJFHGFisXXbnrOx f5U22H3e4sDGi+tn7vgn0mzuD40Q5I6zQAzfcaR8QHaOQYp93pDmgMEBwL7iNxfU7ow7 PR2WKVYi6OEhUG0g4ofuDApFGEJjqoe6vIVQkux7LEB4RstW4Rm/ayI3URIZZVSm+Lhb ugkn9JXOUzkqLm5REsXpCupwBUrkejg65K/qKEfb8jJNtppqCgjYq/K6QZdNgVcYNMgX +i+Q== X-Gm-Message-State: AC+VfDyRylqf/K7bp52cbFUzYU+ldSLwAaYyQjifGOSfWUjICMm7HHtE beFqn1ElO01eXymF10R/meFpCtMBgRA/DyaykJx+cqfkrq+lmruvVWQ= X-Google-Smtp-Source: ACHHUZ7bqjMzmIHSbf0itT5WAVpML6l6eMGu9heZNw8YU/Lsk/iaOtztBcWGFOhaHYSX24KlO3k31LTOcQLPnVnqEr8= X-Received: by 2002:a81:a111:0:b0:568:d586:77c4 with SMTP id y17-20020a81a111000000b00568d58677c4mr10690402ywg.4.1685683361486; Thu, 01 Jun 2023 22:22:41 -0700 (PDT) MIME-Version: 1.0 From: Jayakrishna Vadayath Date: Thu, 1 Jun 2023 22:22:29 -0700 Message-ID: Subject: Implementation of strtok To: libc-alpha@sourceware.org Content-Type: multipart/alternative; boundary="000000000000f7296805fd1ebd7c" X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,HTML_MESSAGE,KAM_DMARC_NONE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000f7296805fd1ebd7c Content-Type: text/plain; charset="UTF-8" Hi, I had a few questions regarding the implementation and documentation of the strtok function. I've noticed the following snippet of code in different applications that parse HTTP packets. ``` char delim[2] = "\r"; void foo(char *p) { char *tmp = p; if (strchr(p, '\r')) tmp = strtok(tmp, delim); if (tmp == NULL) puts("Y"); // (1) This shouldn't happen else puts("N"); // (2) This is the expected case } ``` According to the documentation of the strchr and strtok functions, it would appear that (1) will never be executed. However, I've found one situation that leads to strtok returning a NULL value. If the function foo is invoked as follows, (1) will be executed and the string "Y" will be printed out. ``` char buf[2] = "\r\0"; foo(&buf); ``` After looking at the implementation, I find that this line of code is responsible for it : https://elixir.bootlin.com/glibc/glibc-2.7/source/string/strtok.c#L51 I was wondering why the implementation of strtok tries to skip the leading delimiters and why this edge case is not mentioned in the documentation. I'm looking forward to your answers on this topic. Additionally, I'd be happy to help with fixing the documentation or code if I can. Thank you. -- Regards Jayakrishna Menon --000000000000f7296805fd1ebd7c--