public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Keith Packard <keithp@keithp.com>
To: Alexandre Raymond <alexandre.j.raymond@gmail.com>, newlib@sourceware.org
Subject: Re: Issue with sscanf not parsing correctly
Date: Mon, 16 Aug 2021 09:42:34 -0700	[thread overview]
Message-ID: <87czqdfiit.fsf@keithp.com> (raw)
In-Reply-To: <CAD4yBunQ0L0+=mYr07QADRR8e2bnTLvZAHFc0v2=qUsmTetxfg@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 738 bytes --]

Alexandre Raymond <alexandre.j.raymond@gmail.com> writes:

> Greetings!
>
> I have been developing on a Teensy 4.1 platform, which I'm told uses
> newlib under the hood, and encountered a strange issue with sscanf,
> where different parameter orderings yield different results with
> stdint types.

Odd. I've just tested this using the qemu model for the
thumb_v7e_m_dp_hard gcc target for the picolibc build set to use the
legacy stdio bits from newlib and it seems to work just fine (it also
works fine with the new stdio bits from picolibc).

Could this libc be built to not support C99 formats? That would cause
all kinds of issues as sscanf would fail when it hit the '%hhu' format.

Here's the regular C test code I used for this:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: sscanf-order.c --]
[-- Type: text/x-csrc, Size: 1023 bytes --]

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>

void __assert_func(const char *file, int line, const char *func, const char *expr)
{
    printf("%s:%d (%s) assertion failed: %s\n", file, line, func, expr);
    exit(1);
}

int main(void) {
    uint8_t i2c_addr = 1;
    uint16_t mem_addr = 1;
    uint8_t len = 1;
    int ret;

    printf("%d, %d, %d\n", i2c_addr, mem_addr, len);
    //prints: 1,1,1

    const char *cmd = "1 2 3\n";

    // This is the order I intended to scan the string in
    ret = sscanf(cmd, "%hhu %hu %hhu", &i2c_addr, &mem_addr, &len);

    printf("%d- %d, %d, %d\n", ret, i2c_addr, mem_addr, len);
    assert(i2c_addr ==1 && mem_addr == 2 && len == 3);
    // prints: 3- 1,0,3

    // I just swap the order of the target variables around
    ret = sscanf(cmd, "%hhu %hhu %hu", &i2c_addr, &len, &mem_addr);

    printf("%d- %d, %d, %d\n", ret, i2c_addr, len, mem_addr);
    assert(i2c_addr ==1 && len == 2 && mem_addr == 3);
    // prints: 3- 1,2,3

    return 0;
}

[-- Attachment #1.3: Type: text/plain, Size: 145 bytes --]


I've added this as a picolibc test so it will be regularly checked
against both newlib and picolibc stdio bits going forward.

-- 
-keith

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

      reply	other threads:[~2021-08-16 16:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-16  2:38 Alexandre Raymond
2021-08-16 16:42 ` Keith Packard [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87czqdfiit.fsf@keithp.com \
    --to=keithp@keithp.com \
    --cc=alexandre.j.raymond@gmail.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).