3

I was playing with wmic when this command

wmic<nul 3>nul

produced this:

The strange thing is, any command will work, but only stream 3 causes the output flood.
I googled and searched on SO "cmd output flood" and cannot find any duplicates nor any results.
Question: Why only stream 3 works?

Update: Likewise,

>con 3<con :

completely disables STDOUT!

ScriptKidd
  • 803
  • 1
  • 5
  • 19
  • 2
    Perhaps this is somehow linked to a [problem I once encountered](https://stackoverflow.com/q/42380192)... – aschipfl May 21 '20 at 13:01
  • hang on while i read the answer... – ScriptKidd May 21 '20 at 13:04
  • I don't know if powershell's redirection is the same, but I was able to find this which may be the answer... https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7 – Nico Nekoru May 21 '20 at 14:43
  • 2
    @aschipfl, it is almost exactly the same problem. In this case, after undoing the redirection, stream 0 points to `nul` stream and when the `cmd` prompt tries to read a new command it reads from `nul` (does not fail, just reads nothing), shows prompt again, read again, ... In your case stream 0 ended pointing to a file and when the end of file was reached the `cmd` read operation failed causing the `cmd` instance to close. – MC ND May 21 '20 at 15:22
  • So my guess was right, @MCND, thanks! Now I can't await your answer... ;-) – aschipfl May 21 '20 at 16:15
  • @aschipfl, I think I'm missing something. It would be the same answer I already wrote for your question. – MC ND May 21 '20 at 16:25
  • @MCND, so would you consider this question as a duplicate of mine? I don't want to vote since it's going to be closed immediately due to my batch patch... – aschipfl May 21 '20 at 16:49
  • @aschipfl, I'm in the same case, one vote closes the question. This question is not a duplicate of yours (different behaviour), but the answer is the same. In those cases, after being pointed to the answer, I think it is the OP who should decide to close the question or not. – MC ND May 21 '20 at 16:56
  • @aschipfl, I'm still wondering whether or not to close the question, but I've included an answer with the content of the comment aboout the different behaviour. – MC ND May 21 '20 at 20:18

1 Answers1

4

I think that, as pointed by aschipfl, my answer to their question explains what the source of the problem is.

Please refer to them for the inner details, this answer is just a summary to explain the different behaviour.

  • When a redirection is created the code inside cmd handling this task tries to avoid problems saving the original handles to the streams that are being redirected so the redirection process can be reverted.

  • But the way the used function (standard _dup()) works and the order used when creating the redirection can alter the internal structures used in such a way that at the end the code reversing the redirection uses wrong information.

In short:

<nul 3<nul wmic
  • <nul saves the stdin handle in &3 and assigns nul stream to &0
  • 3<nul saves the handle inside &3 into &4 and assigns nul stream to &3
  • wmic is executed. As it is reading from nul (assigned to &0) it can not read anything and ends.
  • &0 is restored from &3 where it was saved, BUT now &3 points to nul
  • &3 is restored from &4. Now it points to the original stdin

In the original aschipfl's question the observed behaviour was command prompt closing. After code execution

< file1 3< file2 echo/

the redirection cancel process leaves stdin stream pointing to a disk file (file2). cmd starts to read this file for commands to execute (as it normally does from stdin) and at end of file the read operation fails and the cmd instance ends. If you want a convoluted way of seeing it in work to start notepad you can try

( >file1 echo notepad ) & ( <nul 3<file1 break )

But in this question the behaviour is different because the stream assigned to &3, that will be copied to stdin once the redirection is reverted, is not a disk file but nul

<nul 3<nul echo/
  1. Once the command is executed and redirections are reverted stdin stream is associated with nul device
  2. cmd shows the prompt and tries to retrieve a new command reading from nul
  3. The read operation ends but does not retrieve anything so cmd does not execute anything
  4. goto 2

The question "Why only stream 3 works?" (in the sense of it does not work ;) ) is not correct. You can do something like

1>file1 <nul 4<nul echo/

and get the same result using different streams. Or you can do

 3<nul <nul echo/

and it will work (in the sense of it works).

The order in which you request the redirection of the streams, how they are handled by cmd internal code and the existence of still active previous redirections can decide the success or failure of the command.

Compo
  • 36,585
  • 5
  • 27
  • 39
MC ND
  • 69,615
  • 8
  • 84
  • 126