public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Alexandre Raymond <alexandre.j.raymond@gmail.com>
To: newlib@sourceware.org
Subject: Issue with sscanf not parsing correctly
Date: Sun, 15 Aug 2021 22:38:11 -0400	[thread overview]
Message-ID: <CAD4yBunQ0L0+=mYr07QADRR8e2bnTLvZAHFc0v2=qUsmTetxfg@mail.gmail.com> (raw)

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.

Here is a minimal code sample which exhibits the issue:
---8<---

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

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

    Serial.println(String(i2c_addr) + "," + String(mem_addr) + "," +
String(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);
    Serial.println(String(ret) + "- " + String(i2c_addr) + "," +
String(mem_addr) + "," + String(len));
    // 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);
    Serial.println(String(ret) + "- " + String(i2c_addr) + "," +
String(len) + "," + String(mem_addr));
    // prints: 3- 1,2,3
}

void loop() { }

--8<---

Would you have any idea what may be causing this?

Thanks in advance for your help!
Alexandre

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

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

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='CAD4yBunQ0L0+=mYr07QADRR8e2bnTLvZAHFc0v2=qUsmTetxfg@mail.gmail.com' \
    --to=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).