Skip to content

pkg/fatfs: Refactor path handling, minor fixes#8615

Merged
jnohlgard merged 6 commits intoRIOT-OS:masterfrom
jnohlgard:pr/fatfs-paths
Mar 15, 2018
Merged

pkg/fatfs: Refactor path handling, minor fixes#8615
jnohlgard merged 6 commits intoRIOT-OS:masterfrom
jnohlgard:pr/fatfs-paths

Conversation

@jnohlgard
Copy link
Copy Markdown
Member

Contribution description

  • Move multiple identical/similar calls to snprintf to a separate internal function _build_abs_path for better maintainability
  • Remove stale defines FATFS_DIR_SIZE, FATFS_FILE_SIZE
  • Increase FATFS_MAX_VOL_STR_LEN a tiny bit to avoid any possible overflows, fixes a compilation warning/error in GCC 7 about a truncated format string (-Wformat-truncation)
  • Minor editorial changes in Doxygen
  • Word wrap README files for readability in terminal editors

Issues/PRs references

depends on #8614
contains fix for #8265

@jnohlgard jnohlgard added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: pkg Area: External package ports Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation State: waiting for other PR State: The PR requires another PR to be merged first CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Area: fs Area: File systems labels Feb 22, 2018
@jnohlgard jnohlgard added this to the Release 2018.04 milestone Feb 22, 2018
@jnohlgard jnohlgard mentioned this pull request Feb 22, 2018
13 tasks
Copy link
Copy Markdown
Contributor

@MichelRottleuthner MichelRottleuthner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increase FATFS_MAX_VOL_STR_LEN a tiny bit to avoid any possible overflows, fixes a compilation warning/error in GCC 7 about a truncated format string (-Wformat-truncation)

somehow I culdn't reproduce that (using gcc 7.3) and I don't see how 4 is too short for a string like '9:/\0'


/**
* info of a single opened file
* @brief info of a single opened file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while at it, this could be changed to "FatFs file instance descriptor" for consistency with the above

@jnohlgard
Copy link
Copy Markdown
Member Author

@MichelRottleuthner

somehow I culdn't reproduce that (using gcc 7.3) and I don't see how 4 is too short for a string like '9:/\0'

No, but "10:/\0" is too long.
If you are testing you need to be aware that the path refactoring commit hides the warning by removing the small stack allocated buffer in _mount and _umount

@MichelRottleuthner
Copy link
Copy Markdown
Contributor

No, but "10:/\0" is too long.

one could argue that its unlikely to have >9 fat volumes on a riot device, but who knows... But even then, why 6 and not 5?

If you are testing you need to be aware that the path refactoring commit hides the warning by removing the small stack allocated buffer in _mount and _umount

With current master I only get the -Wformat-truncation error when reducing FATFS_MAX_VOL_STR_LEN to 3 (while 4 works just fine for me). Thats why I wondered if it's needed at all.

@jnohlgard
Copy link
Copy Markdown
Member Author

I see this with BOARD=mulle and gcc 7.3.0:

"make" -C /home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs
In file included from /usr/arm-none-eabi/include/stdio.h:800:0,
                 from /home/jgn/work/src/riot/core/include/debug.h:27,
                 from /home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:34:
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c: In function ‘_umount’:
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:73:46: error: ‘:/’ directive output may be truncated writing 2 bytes into a region of size between 1 and 3 [-Werror=format-truncation=]
     snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
                                              ^
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:73:5: note: ‘__builtin_snprintf’ output between 4 and 6 bytes into a destination of size 4
     snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
     ^
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c: In function ‘_mount’:
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:49:46: error: ‘:/’ directive output may be truncated writing 2 bytes into a region of size between 1 and 3 [-Werror=format-truncation=]
     snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
                                              ^
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:49:5: note: ‘__builtin_snprintf’ output between 4 and 6 bytes into a destination of size 4
     snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
     ^
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c: In function ‘_fstat’:
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:254:67: error: ‘__builtin_snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
     snprintf(fs_desc->abs_path_str_buff, FATFS_MAX_ABS_PATH_SIZE, "%d:/%s",
                                                                   ^
/home/jgn/work/src/riot/pkg/fatfs/fatfs_vfs/fatfs_vfs.c:254:5: note: ‘__builtin_snprintf’ output between 4 and 37 bytes into a destination of size 36
     snprintf(fs_desc->abs_path_str_buff, FATFS_MAX_ABS_PATH_SIZE, "%d:/%s",
     ^
cc1: all warnings being treated as errors
make[2]: *** [/home/jgn/work/src/riot/Makefile.base:81: /home/jgn/work/src/riot/tests/pkg_fatfs_vfs/bin/mulle/fatfs_vfs/fatfs_vfs.o] Error 1
Installed toolchain versions
----------------------------
          native gcc: gcc (Gentoo 7.3.0 p1.0) 7.3.0
          msp430-gcc: missing/error
             avr-gcc: avr-gcc (Gentoo 7.3.0 p1.0) 7.3.0
   arm-none-eabi-gcc: arm-none-eabi-gcc (Gentoo 7.3.0 p1.0) 7.3.0
    mips-mti-elf-gcc: mips-mti-elf-gcc (Codescape GNU Tools 2016.05-03 for MIPS MTI Bare Metal) 4.9.2
               clang: clang version 5.0.1 (tags/RELEASE_501/final)
arm-none-eabi-newlib: "2.5.0"
 mips-mti-elf-newlib: "2.1.0"
            avr-libc: "2.0.0" ("20150208")
            cppcheck: Cppcheck 1.81
          coccinelle: spatch version 1.0.6 compiled with OCaml version 4.05.0
                 git: git version 2.16.2

@MichelRottleuthner
Copy link
Copy Markdown
Contributor

the only relevant diff to my setup seems to be the newlib version:

Installed toolchain versions
----------------------------
          native gcc: gcc (GCC) 7.3.0
          msp430-gcc: msp430-gcc (mspgcc_20120406) 4.6.3 20120301 (mspgcc LTS 20120406 unpatched)
             avr-gcc: avr-gcc (GCC) 7.3.0
   arm-none-eabi-gcc: arm-none-eabi-gcc (Arch Repository) 7.3.0
    mips-mti-elf-gcc: mips-mti-elf-gcc (Codescape GNU Tools 2016.05-03 for MIPS MTI Bare Metal) 4.9.2
               clang: clang version 5.0.1 (tags/RELEASE_501/final)
arm-none-eabi-newlib: "3.0.0"
 mips-mti-elf-newlib: "2.1.0"
            avr-libc: "2.0.0" ("20150208")
            cppcheck: Cppcheck 1.82
          coccinelle: spatch version 1.0.6 compiled with OCaml version 4.05.0
                 git: git version 2.16.2

@basilfx
Copy link
Copy Markdown
Member

basilfx commented Mar 14, 2018

#8614 is merged, so Waiting for other PR can be removed.

@jnohlgard jnohlgard removed the State: waiting for other PR State: The PR requires another PR to be merged first label Mar 14, 2018
@jnohlgard
Copy link
Copy Markdown
Member Author

jnohlgard commented Mar 14, 2018

@basilfx thanks

@jnohlgard
Copy link
Copy Markdown
Member Author

Rebased, squashed minor editorial doxygen comment fix into the other doxygen commit.

Copy link
Copy Markdown
Contributor

@cladmi cladmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, just some questions inline.

I did not tested for the moment.


char volume_str[FATFS_MAX_VOL_STR_LEN];
snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
_build_abs_path(fs_desc, "");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this one, you replaced the volume_str by using fs->abs_path_str_buff is it ok to use this one ? Just asking I do not know whet it should be used for.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs->abs_path_str_buff is used as a string buffer for building the absolute paths for the library calls in other parts of the VFS wrapper. Since it is already allocated inside the struct, I didn't see any point in adding another string buffer on the stack, even if it is only a few bytes long.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, from the type definition, it looks like it should be used for this anyway.

_build_abs_path(fs_desc, from_path);

snprintf(fatfs_abs_path_to, sizeof(fatfs_abs_path_to), "%d:/%s",
snprintf(fatfs_abs_path_to, sizeof(fatfs_abs_path_to), "%u:/%s",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This format string is duplicated with _build_abs_path. Would it make sense to put the format string in a shared define to remove the magic value in the middle of the functions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could be macroed, but I don't think it is necessary with only two occurrences. Before this PR, the format string was duplicated in all functions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@cladmi
Copy link
Copy Markdown
Contributor

cladmi commented Mar 14, 2018

Tested with native:

  • tests/pkg_fatfs_vfs everything has OK
  • tests/pkg_fatfs I can create a file and write to it, same way as in master.

Copy link
Copy Markdown
Contributor

@cladmi cladmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, tested only on native.

_build_abs_path(fs_desc, from_path);

snprintf(fatfs_abs_path_to, sizeof(fatfs_abs_path_to), "%d:/%s",
snprintf(fatfs_abs_path_to, sizeof(fatfs_abs_path_to), "%u:/%s",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


char volume_str[FATFS_MAX_VOL_STR_LEN];
snprintf(volume_str, sizeof(volume_str), "%d:/", fs_desc->vol_idx);
_build_abs_path(fs_desc, "");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, from the type definition, it looks like it should be used for this anyway.

@cladmi
Copy link
Copy Markdown
Contributor

cladmi commented Mar 14, 2018

I will test on mulle before merging.

@cladmi
Copy link
Copy Markdown
Contributor

cladmi commented Mar 14, 2018

Hmm, mulle does not have any sdcard so cannot work…

@MichelRottleuthner do you have some hardware to test ?

Copy link
Copy Markdown
Contributor

@MichelRottleuthner MichelRottleuthner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No mulle here, but ran tests/pkg_fatfs_vfs and tests/pkg_fatfs with an SD-card on remote-revb both working fine -> another ACK from my side. Thanks for improving the code @gebart!

@jnohlgard
Copy link
Copy Markdown
Member Author

Thanks for reviewing!

@jnohlgard jnohlgard merged commit 8873a71 into RIOT-OS:master Mar 15, 2018
@jnohlgard jnohlgard deleted the pr/fatfs-paths branch March 15, 2018 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: fs Area: File systems Area: pkg Area: External package ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants