public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* Issue with sscanf not parsing correctly
@ 2021-08-16  2:38 Alexandre Raymond
  2021-08-16 16:42 ` Keith Packard
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandre Raymond @ 2021-08-16  2:38 UTC (permalink / raw)
  To: newlib

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Issue with sscanf not parsing correctly
  2021-08-16  2:38 Issue with sscanf not parsing correctly Alexandre Raymond
@ 2021-08-16 16:42 ` Keith Packard
  0 siblings, 0 replies; 2+ messages in thread
From: Keith Packard @ 2021-08-16 16:42 UTC (permalink / raw)
  To: Alexandre Raymond, newlib


[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-16 16:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-16  2:38 Issue with sscanf not parsing correctly Alexandre Raymond
2021-08-16 16:42 ` Keith Packard

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).