You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/10/13 08:17:18 UTC

[GitHub] [incubator-nuttx-apps] xiaoxiang781216 opened a new pull request #425: Remove all fclose with stdin, stdout and stderr

xiaoxiang781216 opened a new pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425


   ## Summary
   since it is wrong to close the builtin stream and specially note
   https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html:
   
   Since after the call to fclose() any use of stream results in
   undefined behavior, fclose() should not be used on stdin, stdout,
   or stderr except immediately before process termination (see XBD
   Process Termination), so as to avoid triggering undefined behavior
   in other standard interfaces that rely on these streams. If there
   are any atexit() handlers registered by the application, such a
   call to fclose() should not occur until the last handler is
   finishing. Once fclose() has been used to close stdin, stdout, or
   stderr, there is no standard way to reopen any of these streams.
   
   and it is also unnecessary because the stream always get flushed.
   
   ## Impact
   Remove the undefined behaviour
   
   ## Testing
   reported here:
   https://github.com/apache/incubator-nuttx/commit/b0797263ca2817cf536be8b843ac5b5aac5d4d9c
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718317120


   > Very strange, what's value in other FILE's field?
   
   @xiaoxiang781216 
   
   With this PR and https://github.com/apache/incubator-nuttx/pull/1997
   stream->fs_oflags is  0.
   
   ```
   (gdb) p *stream                                                                                                                                                                             
   $1 = {fs_next = 0x7500706d, fs_fd = 8, fs_sem = {semcount = 0}, fs_holder = -32768, fs_counts = 32, fs_bufstart = 0x80000008 "", fs_bufend = 0x0, fs_bufpos = 0x10000010 "\240\v", fs_bufread = 0x0, fs_oflags = 0, fs_flags = 0 '\000', fs_nungotten = 0 '\000', fs_ungotten = "\000"}  
   ```
   
   With nuttx-9.1.0-RC2, stream->fs_oflags is NOT 0
   
   ```
   (gdb) where                                                                                                                                                                                 
   #0  lib_fwrite (ptr=ptr@entry=0x80113c2 <g_nshgreeting>, count=count@entry=1, stream=stream@entry=0x10000484) at stdio/lib_libfwrite.c:71                                                   
   #1  0x0800ea72 in fputs (s=0x80113c2 <g_nshgreeting> "\nNuttShell (NSH) NuttX-9.1.0\n", stream=0x10000484) at stdio/lib_fputs.c:136                                                         
   #2  0x080054a4 in nsh_session (pstate=pstate@entry=0x10000de0, login=login@entry=1 '\001') at nsh_session.c:104                                                                             
   #3  0x08005240 in nsh_consolemain (argc=argc@entry=0, argv=argv@entry=0x0) at nsh_usbconsole.c:349                                                                                          
   #4  0x080050e0 in nsh_main (argc=<optimized out>, argv=<optimized out>) at nsh_main.c:182                                                                                                   
   #5  0x08001e9a in nxtask_start () at task/task_start.c:146                                                                                                                                  
   #6  0x00000000 in ?? ()                                                                                                                                                                     
   (gdb) p *stream                                                                                                                                                                             
   $2 = {fs_fd = 1, fs_sem = {semcount = 1}, fs_holder = 0, fs_counts = 0, fs_bufstart = 0x100018b0 "", fs_bufend = 0x100018f0 "mkrd    P", fs_bufpos = 0x100018b0 "", fs_bufread = 0x100018b0 "", fs_oflags = 22, fs_flags = 4 '\004', fs_nungotten = 0 '\000', fs_ungotten = "\000"}    
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718340027


   > Campare the data, it look like that stream was freed which is what this patch want to fix.
   
   @xiaoxiang781216 
   Hmmm, I added breakpoints to fclose() and close() and ran the NuttX on stm32d4discovery again.
   However, close(fd=3)  was only hit which is not stdout.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-708432008


   This fixes the USB console for me, but note that this is still a workaround. The following strictly conforming C program causes a crash on NuttX (tested in Flat mode build):
   ```
   #include <stdio.h>
   
   int main(void)
   {
       /* Note: order significant if want to see a crash on NuttX. */
       fclose(stdin);
       fclose(stdout);
       fclose(stderr);
       /* ... open some file here, read/write to it, close it. ... just don't call printf() */
       return 0;
   }
   ```
   
   This kind of usage of fclose() is unlikely to appear in real programs, though.
   
   Also note that fflush(stdin) is undefined behavior in C, but we can allow it since we are the implementor.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717815230


   > > Done, both rebase to the latest master.
   > 
   > @xiaoxiang781216
   > I think you pushed the commit to https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   > However, this PR is from https://github.com/apache/incubator-nuttx-apps/tree/fclose
   
   @xiaoxiang781216 
   
   I tried 
   https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   and
   https://github.com/xiaoxiang781216/incubator-nuttx/tree/fclose3
   with stm32f4discovery:usbnsh
   
   Then start terminal program on Ubuntu 18.04
   $ picocom -b 115200 /dev/ttyACM0
   
   However, lib_fwrite() always fails.
   
   ```
   (gdb) where                                                                                                                                                                                     
   #0  lib_fwrite (ptr=ptr@entry=0x8011590 <g_nshgreeting>, count=29, stream=stream@entry=0x200012fc) at stdio/lib_libfwrite.c:81                                                                  
   #1  0x0800f12a in fputs (s=0x8011590 <g_nshgreeting> "\nNuttShell (NSH) NuttX-8.2.0\n", stream=0x200012fc) at stdio/lib_fputs.c:171                                                             
   #2  0x08005b40 in nsh_session (pstate=0x10000bd0, login=<optimized out>, argc=1, argv=0x10000b88) at nsh_session.c:106                                                                          
   #3  0x080058ce in nsh_consolemain (argc=argc@entry=1, argv=argv@entry=0x10000b88) at nsh_usbconsole.c:334                                                                                       
   #4  0x080057b8 in nsh_main (argc=1, argv=0x10000b88) at nsh_main.c:168                                                                                                                          
   #5  0x08003252 in nxtask_startup (entrypt=0x800578d <nsh_main>, entrypt@entry=0x20000188 <inited>, argc=1, argv=0x10000b88) at sched/task_startup.c:165                                         
   #6  0x08001cdc in nxtask_start () at task/task_start.c:144                                                                                                                                      
   #7  0x00000000 in ?? () 
   
   
   ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)                                                                                                                         
   #ifndef CONFIG_STDIO_DISABLE_BUFFERING                                                                                                                                                          
   {                                                                                                                                                                                               
     FAR const unsigned char *start = ptr;                                                                                                                                                         
     FAR const unsigned char *src   = ptr;                                                                                                                                                         
     ssize_t ret = ERROR;                                                                                                                                                                          
     unsigned char *dest;                                                                                                                                                                          
                                                                                                                                                                                                   
     /* Make sure that writing to this stream is allowed */                                                                                                                                        
                                                                                                                                                                                                   
     if (stream == NULL)                                                                                                                                                                           
       {                                                                                                                                                                                           
         set_errno(EBADF);                                                                                                                                                                         
         return ret;                                                                                                                                                                               
       }                                                                                                                                                                                           
                                                                                                                                                                                                   
     /* Check if write access is permitted */                                                                                                                                                      
                                                                                                                                                                                                   
     if ((stream->fs_oflags & O_WROK) == 0)                                                                                                                                                        
       {                                                                                                                                                                                           
   =>    set_errno(EBADF);                                                                                                                                                                         
         goto errout;                                                                                                                                                                              
       }   
   
   (gdb) p stream->fs_oflags                                                                                                                                                                       
   $5 = 0                        
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 merged pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 merged pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717162577


   >This fixes the USB console for me, but note that this is still a workaround. 
   
   @juniskane @xiaoxiang781216 
   Hmm, as long as I tried this PR after rebasing to master, 
   it still does not work with stm32f4discovery:usbnsh.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718700018


   > @masayuki2009 could you merge this PR and [apache/incubator-nuttx#1997](https://github.com/apache/incubator-nuttx/pull/1997)?
   
   Both PRs are merged.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718330194


   Campare the data, it look like that stream is freed which is what this patch want to fix.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717807067


   > Done, both rebase to the latest master.
   
   @xiaoxiang781216 
   I think you pushed the commit to https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   However, this PR is from https://github.com/apache/incubator-nuttx-apps/tree/fclose
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718696964


   @masayuki2009 could you merge this PR and https://github.com/apache/incubator-nuttx/pull/1997?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717927282


   @xiaoxiang781216 would you please set this a `draft` until it is sorted out?. You can test on the board I left with you, it has a usb.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717923752


   Very strange, what's value in other FILE's field?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718330194


   Campare the data, it look like that stream is freed.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717815230


   > > Done, both rebase to the latest master.
   > 
   > @xiaoxiang781216
   > I think you pushed the commit to https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   > However, this PR is from https://github.com/apache/incubator-nuttx-apps/tree/fclose
   
   @xiaoxiang781216 
   
   I tried 
   https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   and
   https://github.com/xiaoxiang781216/incubator-nuttx/tree/fclose3
   with stm32f4discovery:usbnsh
   
   Then start terminal program on Ubuntu 18.04
   $ picocom -b 115200 /dev/ttyACM0
   
   Howerver, lib_fwrite() always failes.
   
   ```
   gdb) where                                                                                                                                                                                     
   #0  lib_fwrite (ptr=ptr@entry=0x8011590 <g_nshgreeting>, count=29, stream=stream@entry=0x200012fc) at stdio/lib_libfwrite.c:81                                                                  
   #1  0x0800f12a in fputs (s=0x8011590 <g_nshgreeting> "\nNuttShell (NSH) NuttX-8.2.0\n", stream=0x200012fc) at stdio/lib_fputs.c:171                                                             
   #2  0x08005b40 in nsh_session (pstate=0x10000bd0, login=<optimized out>, argc=1, argv=0x10000b88) at nsh_session.c:106                                                                          
   #3  0x080058ce in nsh_consolemain (argc=argc@entry=1, argv=argv@entry=0x10000b88) at nsh_usbconsole.c:334                                                                                       
   #4  0x080057b8 in nsh_main (argc=1, argv=0x10000b88) at nsh_main.c:168                                                                                                                          
   #5  0x08003252 in nxtask_startup (entrypt=0x800578d <nsh_main>, entrypt@entry=0x20000188 <inited>, argc=1, argv=0x10000b88) at sched/task_startup.c:165                                         
   #6  0x08001cdc in nxtask_start () at task/task_start.c:144                                                                                                                                      
   #7  0x00000000 in ?? () 
   
   
   ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)                                                                                                                         
   #ifndef CONFIG_STDIO_DISABLE_BUFFERING                                                                                                                                                          
   {                                                                                                                                                                                               
     FAR const unsigned char *start = ptr;                                                                                                                                                         
     FAR const unsigned char *src   = ptr;                                                                                                                                                         
     ssize_t ret = ERROR;                                                                                                                                                                          
     unsigned char *dest;                                                                                                                                                                          
                                                                                                                                                                                                   
     /* Make sure that writing to this stream is allowed */                                                                                                                                        
                                                                                                                                                                                                   
     if (stream == NULL)                                                                                                                                                                           
       {                                                                                                                                                                                           
         set_errno(EBADF);                                                                                                                                                                         
         return ret;                                                                                                                                                                               
       }                                                                                                                                                                                           
                                                                                                                                                                                                   
     /* Check if write access is permitted */                                                                                                                                                      
                                                                                                                                                                                                   
     if ((stream->fs_oflags & O_WROK) == 0)                                                                                                                                                        
       {                                                                                                                                                                                           
   =>    set_errno(EBADF);                                                                                                                                                                         
         goto errout;                                                                                                                                                                              
       }                         
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-709052915


   stdin/stdout/stderr are still freed when the task exit, only fclose skip these special handlers.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718398878


   @xiaoxiang781216 
   Finally, I found bugs in nsh_usbconsole.c and fixed locally.
   
   I think we need to set pstate->cn_outstream and pstete->cnerrstream with fdopen()
   which is actually done in nsh_atlconsle.c
   I'm still not sure why stm32f4discovery:usbnsh works with nuttx-9.1.0-RC2.
   
   May I add the coomit to this PR?
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-707576241


   @juniskane could you try whether this patch can fix your issue?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718317120


   > Very strange, what's value in other FILE's field?
   
   @xiaoxiang781216 
   
   (gdb) p *stream                                                                                                                                                                             
   $1 = {fs_next = 0x7500706d, fs_fd = 8, fs_sem = {semcount = 0}, fs_holder = -32768, fs_counts = 32, fs_bufstart = 0x80000008 "", fs_bufend = 0x0, fs_bufpos = 0x10000010 "\240\v", fs_bufread = 0x0, fs_oflags = 0, fs_flags = 0 '\000', fs_nungotten = 0 '\000', fs_ungotten = "\000"}  
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#discussion_r505547525



##########
File path: graphics/nxwm/src/cnxterm.cxx
##########
@@ -500,27 +500,18 @@ int CNxTerm::nxterm(int argc, char *argv[])
   // console).  (2) Don't bother trying to put debug instrumentation in the
   // following because it will end up in the NxTerm window.
 
-  std::fflush(stdout);
-  std::fflush(stderr);
-
 #ifdef CONFIG_NXTERM_NXKBDIN
-  std::fclose(stdin);
+  std::fflush(stdin);

Review comment:
       Using fflush with stdin is not appropriate.  fflush has no effect on input streams.  See https://pubs.opengroup.org/onlinepubs/007904975/functions/fflush.html
   
   It would be better to remove this and all other places where fflush is used with stdin.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717797831


   Done, both rebase to the latest master.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717815230


   > > Done, both rebase to the latest master.
   > 
   > @xiaoxiang781216
   > I think you pushed the commit to https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   > However, this PR is from https://github.com/apache/incubator-nuttx-apps/tree/fclose
   
   @xiaoxiang781216 
   
   I tried 
   https://github.com/xiaoxiang781216/incubator-nuttx-apps/tree/fclose
   and
   https://github.com/xiaoxiang781216/incubator-nuttx/tree/fclose3
   with stm32f4discovery:usbnsh
   
   Then start terminal program on Ubuntu 18.04
   $ picocom -b 115200 /dev/ttyACM0
   
   However, lib_fwrite() always fails.
   
   ```
   (gdb) where                                                                                                                                                                                     
   #0  lib_fwrite (ptr=ptr@entry=0x8011590 <g_nshgreeting>, count=29, stream=stream@entry=0x200012fc) at stdio/lib_libfwrite.c:81                                                                  
   #1  0x0800f12a in fputs (s=0x8011590 <g_nshgreeting> "\nNuttShell (NSH) NuttX-8.2.0\n", stream=0x200012fc) at stdio/lib_fputs.c:171                                                             
   #2  0x08005b40 in nsh_session (pstate=0x10000bd0, login=<optimized out>, argc=1, argv=0x10000b88) at nsh_session.c:106                                                                          
   #3  0x080058ce in nsh_consolemain (argc=argc@entry=1, argv=argv@entry=0x10000b88) at nsh_usbconsole.c:334                                                                                       
   #4  0x080057b8 in nsh_main (argc=1, argv=0x10000b88) at nsh_main.c:168                                                                                                                          
   #5  0x08003252 in nxtask_startup (entrypt=0x800578d <nsh_main>, entrypt@entry=0x20000188 <inited>, argc=1, argv=0x10000b88) at sched/task_startup.c:165                                         
   #6  0x08001cdc in nxtask_start () at task/task_start.c:144                                                                                                                                      
   #7  0x00000000 in ?? () 
   
   
   ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)                                                                                                                         
   #ifndef CONFIG_STDIO_DISABLE_BUFFERING                                                                                                                                                          
   {                                                                                                                                                                                               
     FAR const unsigned char *start = ptr;                                                                                                                                                         
     FAR const unsigned char *src   = ptr;                                                                                                                                                         
     ssize_t ret = ERROR;                                                                                                                                                                          
     unsigned char *dest;                                                                                                                                                                          
                                                                                                                                                                                                   
     /* Make sure that writing to this stream is allowed */                                                                                                                                        
                                                                                                                                                                                                   
     if (stream == NULL)                                                                                                                                                                           
       {                                                                                                                                                                                           
         set_errno(EBADF);                                                                                                                                                                         
         return ret;                                                                                                                                                                               
       }                                                                                                                                                                                           
                                                                                                                                                                                                   
     /* Check if write access is permitted */                                                                                                                                                      
                                                                                                                                                                                                   
     if ((stream->fs_oflags & O_WROK) == 0)                                                                                                                                                        
       {                                                                                                                                                                                           
   =>    set_errno(EBADF);                                                                                                                                                                         
         goto errout;                                                                                                                                                                              
       }                         
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-708997593


   fflush(stdin) is in comp.lang.c FAQ:
   http://c-faq.com/stdio/stdinflush.html
   
   Chapter and verse is ISO/IEC 9899:1999 section 7.19.5.2 The fflush function, clause 2:
   _"If stream points to an output stream or an update stream in which the most recent
   operation was not input, the fflush function causes any unwritten data for that stream
   to be delivered to the host environment to be written to the file; otherwise, the behavior is
   undefined."_
   
   Some systems indeed define fflush(stdin) to flush out the unread data in the buffer, it is just not portable. We can keep doing that in NuttX, if we wish.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] jerpelea commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
jerpelea commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-707576924


   LGTM


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718423964


   >I'm still not sure why stm32f4discovery:usbnsh works with nuttx-9.1.0-RC2.
   
   @xiaoxiang781216 
   I think the previous code in nsh_usbconsole.c worked luckly, because file_struct was statically allocated.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717756806


   @masayuki2009 I don't have the device with USB support:(, @juniskane could you help retest the change?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on a change in pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#discussion_r505564741



##########
File path: graphics/nxwm/src/cnxterm.cxx
##########
@@ -500,27 +500,18 @@ int CNxTerm::nxterm(int argc, char *argv[])
   // console).  (2) Don't bother trying to put debug instrumentation in the
   // following because it will end up in the NxTerm window.
 
-  std::fflush(stdout);
-  std::fflush(stderr);
-
 #ifdef CONFIG_NXTERM_NXKBDIN
-  std::fclose(stdin);
+  std::fflush(stdin);

Review comment:
       Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718411052


   Sure, no problem. Thanks for help me finding the touch bug.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-708432008


   This fixes the USB console for me, but note that this is still a workaround. The following strictly conforming C program causes a crash on NuttX (tested in Flat mode build):
   ```
   #include <stdio.h>
   
   int main(void)
   {
       /* Note: order significant if want to see a crash on NuttX. */
       fclose(stdin);
       fclose(stdout);
       fclose(stderr);
       /* ... can open some file here, read/write to it, close it. ... just don't call printf() */
       return 0;
   }
   ```
   
   This kind of usage of fclose() is unlikely to appear in real programs, though.
   
   Also note that fflush(stdin) is undefined behavior in C, but we can allow it since we are the implementor.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-709038345


   > There might be difficulties with setbuf() and setvbuf(), however, if application sets a buffer in stack and relied on fclose() disassociating it from the stream before exiting code block were buffer was defined. This is probably very rare scenario.
   
   Actually that case is likely fine, as any operation on stream after fclose() is UB anyway so it does not matter if the IO buffer vanishes silently. Nothing in nuttx libc touches the buffer without user calling a function first. Same applies to re-use of the buffer for some new stream by libc user. But automatically allocated buffer might leak memory (until task exit) if fclose on stdin/stdout/stderr does not free it.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718317120


   > Very strange, what's value in other FILE's field?
   
   @xiaoxiang781216 
   
   With this PR and https://github.com/apache/incubator-nuttx/pull/1997
   stream->fs_oflags is  0.
   
   ```
   (gdb) p *stream                                                                                                                                                                             
   $1 = {fs_next = 0x7500706d, fs_fd = 8, fs_sem = {semcount = 0}, fs_holder = -32768, fs_counts = 32, fs_bufstart = 0x80000008 "", fs_bufend = 0x0, fs_bufpos = 0x10000010 "\240\v", fs_bufread = 0x0, fs_oflags = 0, fs_flags = 0 '\000', fs_nungotten = 0 '\000', fs_ungotten = "\000"}  
   ```
   
   With nuttx-9.1.0-RC2, stream->fs_oflags is not 0
   
   ```
   (gdb) where                                                                                                                                                                                 
   #0  lib_fwrite (ptr=ptr@entry=0x80113c2 <g_nshgreeting>, count=count@entry=1, stream=stream@entry=0x10000484) at stdio/lib_libfwrite.c:71                                                   
   #1  0x0800ea72 in fputs (s=0x80113c2 <g_nshgreeting> "\nNuttShell (NSH) NuttX-9.1.0\n", stream=0x10000484) at stdio/lib_fputs.c:136                                                         
   #2  0x080054a4 in nsh_session (pstate=pstate@entry=0x10000de0, login=login@entry=1 '\001') at nsh_session.c:104                                                                             
   #3  0x08005240 in nsh_consolemain (argc=argc@entry=0, argv=argv@entry=0x0) at nsh_usbconsole.c:349                                                                                          
   #4  0x080050e0 in nsh_main (argc=<optimized out>, argv=<optimized out>) at nsh_main.c:182                                                                                                   
   #5  0x08001e9a in nxtask_start () at task/task_start.c:146                                                                                                                                  
   #6  0x00000000 in ?? ()                                                                                                                                                                     
   (gdb) p *stream                                                                                                                                                                             
   $2 = {fs_fd = 1, fs_sem = {semcount = 1}, fs_holder = 0, fs_counts = 0, fs_bufstart = 0x100018b0 "", fs_bufend = 0x100018f0 "mkrd    P", fs_bufpos = 0x100018b0 "", fs_bufread = 0x100018b0 "", fs_oflags = 22, fs_flags = 4 '\004', fs_nungotten = 0 '\000', fs_ungotten = "\000"}    
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-719154255


   @btashton this patch need backport to 10.0.0 too.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#discussion_r505547525



##########
File path: graphics/nxwm/src/cnxterm.cxx
##########
@@ -500,27 +500,18 @@ int CNxTerm::nxterm(int argc, char *argv[])
   // console).  (2) Don't bother trying to put debug instrumentation in the
   // following because it will end up in the NxTerm window.
 
-  std::fflush(stdout);
-  std::fflush(stderr);
-
 #ifdef CONFIG_NXTERM_NXKBDIN
-  std::fclose(stdin);
+  std::fflush(stdin);

Review comment:
       Using fflush with stdin is not appropriate.  fflush has no effect on input streams.  See https://pubs.opengroup.org/onlinepubs/007904975/functions/fflush.html
   
   It would be better to remove this and all other places where fflush is used with stdin since it does nothing.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-708995071


   Calling fclose() on any of the standard text streams is not undefined behavior, just any use of the stream afterwards (including second fclose() and things like perror() inside library functions, but not the implicit closing of files in process exit). Wording in ISO/IEC 9899:1999 7.19.3.4 hints that standard streams can be closed like any other: _"The value of a pointer to a FILE object is indeterminate after the associated file is closed (including the standard text streams)."_
   
   OpenGroup/POSIX bug tracker has interesting discussion about this:
   https://www.austingroupbugs.net/view.php?id=555
   That shows how the recently added wording you quoted from OpenGroup spec came into being.
   
   The code that was in USB console and daemon() is typical for many UNIX daemons so existing applications might contain a similar fclose/dup2/fdopen sequence.
   
   Gnulib has close_stdout() explicitly for closing stdout and stderr streams, I guess someone finds that feature useful:
   https://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/closeout.c
   
   Just making fclose() a no-op if applied to any of the three standard streams feels like a suitable fix for this. There might be difficulties with setbuf() and setvbuf(), however, if application sets a buffer in stack and relied on fclose() disassociating it from the stream before exiting code block were buffer was defined. This is probably very rare scenario.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-708586521


   > This fixes the USB console for me, but note that this is still a workaround.
   
   No, it isn't a workaround, the old way is an undefined behaviour from https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html and we should avoid. 
   
   > The following strictly conforming C program causes a crash on NuttX (tested in Flat mode build):
   > 
   > ```
   > #include <stdio.h>
   > 
   > int main(void)
   > {
   >     /* Note: order significant if want to see a crash on NuttX. */
   >     fclose(stdin);
   >     fclose(stdout);
   >     fclose(stderr);
   >     /* ... can open some file here, read/write to it, close it. ... just don't call printf() */
   >     return 0;
   > }
   > ```
   > 
   > This kind of usage of fclose() is unlikely to appear in real programs, though.
   
   To improve the compability, I will provide a patch to ignore fclose to stdin/stdout/stderr anyway.
   
   > 
   > Also note that fflush(stdin) is undefined behavior in C, but we can allow it since we are the implementor.
   
   Why is fflush(stdin) undefined? can you point me the related document? It's reasonable to expect that fflush(stdin) flush out the unread data in the buffer.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718330194


   Campare the data, it look like that stream was freed which is what this patch want to fix.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] juniskane commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
juniskane commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717768288


   nshlib/nsh_altconsole.c calls non-existent flush(), typo for fflush().
   
   Other than this, change works for me. Tested with proprietary board. I'm using slightly older apps and backported the change though.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 edited a comment on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 edited a comment on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-718398878


   @xiaoxiang781216 
   Finally, I found bugs in nsh_usbconsole.c and fixed locally.
   
   I think we need to set pstate->cn_outstream and pstete->cnerrstream with fdopen()
   which is actually done in nsh_atlconsle.c
   I'm still not sure why stm32f4discovery:usbnsh works with nuttx-9.1.0-RC2.
   
   May I add the commit to this PR?
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717777756


   > nshlib/nsh_altconsole.c calls non-existent flush(), typo for fflush().
   > 
   > Other than this, change works for me. Tested with proprietary board. I'm using slightly older apps and backported the change though.
   
   My fault, @masayuki2009 could you test my new change? I am wondering why the PR pass the build without error or warning.
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] masayuki2009 commented on pull request #425: Remove all fclose with stdin, stdout and stderr

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #425:
URL: https://github.com/apache/incubator-nuttx-apps/pull/425#issuecomment-717783143


   > > nshlib/nsh_altconsole.c calls non-existent flush(), typo for fflush().
   > > Other than this, change works for me. Tested with proprietary board. I'm using slightly older apps and backported the change though.
   > 
   > My fault, @masayuki2009 could you test my new change? I am wondering why the PR pass the build without error or warning.
   
   @xiaoxiang781216 
   
   At least, stm32f4discovery:usbnsh does not use nsh_altconsole.c.
   Anyway, could you fix the file and rebase the fclose branch to the latest master and push the commit again?
   
   Also, I think we need to apply https://github.com/apache/incubator-nuttx/pull/1997 as well.
   If so, please rebase the  xiaoxiang781216:fclose3 to the latest master.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org