Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tinystdio: Fix missing error report in underlying close function of fclose #18

Merged
merged 31 commits into from
Jul 28, 2024

Conversation

HanaMAshour
Copy link
Collaborator

The __bufio_close() function used to discard the return value of the semihost, so this return value is now saved in a variable to be returned. It Will return -1 if the file has been closed before.

keith-packard and others added 23 commits July 10, 2024 13:15
The original floating point output code relied on 'DTOA_CARRY'
semantics where the low-level conversion code would punt some rounding
operations up to vfprintf. This caused double rounding, and when the
fraction rounded up to 0.5, the vfprintf rounding would round that
again to 1.0.

All of the conversion functions now perform correct rounding
internally, so the DTOA_CARRY mechanism is no longer necessary.

This also allows the conversion functions to return zero digits when
the result rounds to zero as vfprintf no longer looks at the first
digit to deal with DTOA_CARRY stuff.

Signed-off-by: Keith Packard <[email protected]>
Make sure values near 0.5 times a power of ten are rounded correctly
in %f mode.

Signed-off-by: Keith Packard <[email protected]>
The two allocators mallocr.c and nano-mallocr.c mostly support the
same set of functions, but posix_memalign is an exception:
nano-mallocr has an implementation of it, but mallocr doesn't. This
means the available API changes depending on whether you asked for a
fast or a small memory allocator.

mallocr does support aligned memory allocation, via other APIs. So
posix_memalign can be implemented as a wrapper on the existing API,
just as it is in nano-mallocr.
When including our private _ansi.h header file from public headers,
use "_ansi.h" to direct the preprocessor to look in the same directory
instead of potentially discovering a conflicting external header file.

Signed-off-by: Keith Packard <[email protected]>
When building in strict C mode, stop exposing POSIX-specific types
(off_t and ssize_t) and non-C functions (__getdelim and __getline).

For tinystdio, this means switching from sys/types.h to sys/_types.h
and then adding fragments from sys/types.h that define off_t and
ssize_t for POSIX applications.

Signed-off-by: Keith Packard <[email protected]>
As the include files are fixed to limit symbols to those specified by
POSIX, the semihost code needs to stop expecting unintended symbol
definitions.

Signed-off-by: Keith Packard <[email protected]>
This file uses size_t, which is defined in sys/types.h

Signed-off-by: Keith Packard <[email protected]>
This header was assuming stdint.h and sys/types.h had been included.

Signed-off-by: Keith Packard <[email protected]>
Go through all of the standard C language headers and ensure that
they don't expose undefined symbols either directly or indirectly
by including other files.

Also review some other headers, such as <ieeefp.h> and trim them down
to their relevant standards.

Use C's '_Noreturn' keyword instead of _ATTRIBUTE((__noreturn__)).
Add include/stdnoreturn.h as required by C.

Add #includes and #define _*_SOURCE as needed in source files to get
the necessary symbols which were only accidentally defined before.

Signed-off-by: Keith Packard <[email protected]>
Instead of using a stub for _user_strerror, define it as 'weak' and
check for NULL.

Signed-off-by: Keith Packard <[email protected]>
The glibc test suite uses fstat64, so add that to the semihost code
and declare it in sys/stat.h

Signed-off-by: Keith Packard <[email protected]>
I didn't see any bug fixes here, but it's nice to have the source code
back in sync.

Signed-off-by: Keith Packard <[email protected]>
32-bit floats need half a bit from the first digit past 9
digits. Check the first digit past that limit and use it to round the
significand by incrementing when the digit is >= 5.

Signed-off-by: Keith Packard <[email protected]>
When tempnam failed to find a suitable name, it didn't bother freeing
the buffer.

Signed-off-by: Keith Packard <[email protected]>
memcloser shrinks the buffer, but the analyzer can't figure out
that it hasn't leaked memory as a result.

Signed-off-by: Keith Packard <[email protected]>
This code is full of twisty array usage and the analyzer gets
confused.  Disable a bunch of analyzer warnings and then initialize a
bunch of arrays to zero.

Signed-off-by: Keith Packard <[email protected]>
Remove unused code paths for non-picolibc targets. Remove hand-coded
malloc.h bits. Remove non-stdc declarations and types.

Signed-off-by: Keith Packard <[email protected]>
glibc uses the special value 3752432815e-39 to test strtod, strtof and
sscanf. In hex, it's just a bit more than 0x1.306efbp-98 while
3752432815e-39 is just a bit less.  Doing a conversion to float should
yield 0x1.306efcp-98, but slighty incautious code might generate
0x1.306efap-98 instead.

Signed-off-by: Keith Packard <[email protected]>
Make sure the library and tests build without errors.

Signed-off-by: Keith Packard <[email protected]>
Change the dash to equal.

Signed-off-by: Keith Packard <[email protected]>
After this change, %a & %A is correctly handled in vfscanf

Signed-off-by: Ahmed Shehab <[email protected]>
@keith-packard
Copy link
Collaborator

The title of this PR is confusing -- the bug is that an error in the underlying close function won't be reported, not that the library should catch and report errors if fclose is called more than once (which is undefined behavior according to POSIX).

A-Shehab and others added 4 commits July 14, 2024 09:44
issue found by running SuperTest by Solid Sands

Signed-off-by: Ahmed Shehab <[email protected]>
%n: It assigns a variable the count of the number of characters used in the
print statement before the occurence of '%n'

before this change '%n' format specifier was not handled in tinystdio.
Therefore, they were treated as normal characters resulting in wrong character
count when used.

Add a new meson option, -Dprintf-percent-n, which controls whether this feature
is enabled or not. This is false by default.

Signed-off-by: Ahmed Shehab <[email protected]>
Added testcase that calls sprintf using "%n" format specifier.
issue found by sunning SuperTest by Solid Sands

Signed-off-by: Ahmed Shehab <[email protected]>
Make sure this code gets covered

Signed-off-by: Keith Packard <[email protected]>
@HanaMAshour HanaMAshour changed the title tinystdio: fclose issue if called more than once tinystdio: Fix missing error report in underlying close function of fclose Jul 15, 2024
mostafa-salmaan and others added 2 commits July 15, 2024 10:53
`freopen` should reset the unget buffer to ensure subsequent input
operations such as scanf work correctly.  The unget buffer was not
being reset, causing issues when tests are run consecutively. Using
the atomic operation to reset the `unget` buffer to ensure subsequent
input operations such as scanf work correctly.

Signed-off-by: Mostafa Salman <[email protected]>
The conversion returns the number of fraction digits generated (ndig -
1); we need to remember the number of fraction digits desired in case that
is larger so we can pad with zeros.

Reported-by: Ahmed Shehab <[email protected]>
Signed-off-by: Keith Packard <[email protected]>
@HanaMAshour HanaMAshour force-pushed the hanaa_fclose_issue branch 2 times, most recently from d52a7d5 to 8a3862f Compare July 16, 2024 10:36
The __bufio_close() function used to discard the return value of the semihost,
so this return value is now saved in a variable to be returned.
It Will return -1 if the file has been closed before.

Signed-off-by: Hana Ashour <[email protected]>
@mostafa-salmaan mostafa-salmaan merged commit d2f70dd into main Jul 28, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants