NIOS Library bug in scan functions
The scan functions contain a bug that affects time zone routines, but could affect other routines as well. The bug is in both the PRO and STD versions of NIOS software versions 19 and above.
This bug does not appear to be in the publicly available versions of NEWLIB, just the Intel implementation.
The bug can be exposed with the following code:
char result[11];
int n;
sscanf ("ABC+XYZ", "%10[^+]%n", result, &n);
printf("Result string = '%s', Number of characters consumed = %d\n", &result, n);
The code above will return incorrect results:
Result string = 'ABC', Number of characters consumed = 6
The result string is correct but the number of characters consumed should be 3 not 6.
The file with the bug is vfscanf.c and it can be found in the following location which is dependent on the version of software installed:
C:\intelFPGA_pro\22.2\nios2eds\bin\gnu\src\newlib-4.1.0\newlib\libc\stdio\vfscanf.c
Around line 1196, this sequence of code can be found:
#ifdef _WANT_IO_POSIX_EXTENSIONS
shrink_m_ptr (char, p_p, n + 1, p_siz);
#endif
nassigned++;
nread += n;
}
nread += n; <<<<==== REMOVE THIS LINE
break;
case CT_STRING:
/* like CCL, but zero-length string OK, & no NOSKIP */
if (width == 0)
width = SIZE_MAX;
The line indicated in the code fragment should be removed. After editing the vscanf.c file, you will need to rebuild the library which can be done by cleaning the BSP project then building it again.