PaulStoffregen / cores

Teensy Core Libraries for Arduino
522 stars 374 forks source link

Issue with sscanf not parsing correctly #592

Open ajraymond opened 3 years ago

ajraymond commented 3 years ago

Using:

I have narrowed down an issue I am having with sscanf to the following snippet of code.

Depending on the order of my variables in the format string and arguments, the results are different.

#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() { }
FrankBoesing commented 3 years ago

Scanf is from the newlib library. If you think there is a bug, report it there.

https://sourceware.org/newlib/

ajraymond commented 3 years ago

I have started a thread at https://sourceware.org/pipermail/newlib/2021/018546.html

They are asking if newlib is built with C99 support in Teensy.

FrankBoesing commented 3 years ago

It uses -std=gnu++14

FrankBoesing commented 3 years ago

Hm, may be it was fixed already... did not think of that, sorry. Teensy uses a pretty old version.

You can try to update to gcc10 (+newer newlib) to see if that changes anything: https://github.com/TeensyUser/doc/wiki/GCC#Switching-between-different-toolchains