0

I have a program that reads in input from the user and then prints out the memory location of where the input is stored in memory. It then asks if the users wishes to continue. When "Y" is entered, the program waits again for user input. If "N" is entered, the program exits. Here is a sample of how the program works.

Give me data to dump: ABCD 0xbeb9eaf8: 41 42 43 44 Dump again (y/n): y Give me data to dump: ADDD 0xbeb9eaf8: 41 44 44 44 Dump again (y/n): n 

When I try to feed python output into the program instead using the command

 echo `python -c 'print "A"'` | ./program 

It results in an infinite loop like this:

Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 Dump again (y/n): Give me data to dump: 0xbeb31af8: 41 

How do I pipe the python output to the program so that this infinite loop does not occur? The architecture is arm.

6
  • 3
    I think ./program's reading logic is broken. Does it work if you provide the n as well?CommentedFeb 20, 2018 at 7:40
  • On a side note: python -c 'print "A"' is perfectly capable of writing to stdout, you don't need to wrap it in echo `...`.CommentedFeb 20, 2018 at 7:52
  • I tried putting 'n' too but the program does not recognise it as an exit request..CommentedFeb 20, 2018 at 8:09
  • The echo/python statement you wrote is equivalent to the user typing A followed by a newline followed by an infinite number of Control-D characters. Can you verify that your program does the same thing when you do that?CommentedFeb 20, 2018 at 9:23
  • Yes I tried entering A,newline, followed by an infinite number of Control-D and I get the same exact output. What could be causing these 'Contrl-D' characters?CommentedFeb 20, 2018 at 9:38

1 Answer 1

0

Without seeing the code, this is conjecture, but...

The program doing the reading probably has code that tries to read from stdin with a lot of the equivalent of try/catch blocks (C#-based pseudo-code):

string memoryLocationToDisplay; while(true) { try { Console.Write("Give me data to dump: ") string input = Console.ReadLine(); if (IsValidMemoryLocation(input)) { memoryLocationToDisplay = input; } catch {} PrintMemoryLocation(memoryLocationToDisplay) try { Console.Write("Dump again (y/n): ") if Console.ReadLine().ToLower() == "n" { exit; } } catch {} } 

However, the only character the program is reading is ^d (End of File), which functionally results in an infinite loop with the same memory location being printed out forever.

This is a bug in the program doing the reading, but it's probably an easy enough one to work around:

printf "A\nn\n" | ./program 

the printf bit will print out

A<newline> n<newline> 

which then gets piped through the program, so it now sees the 'n' and a newline, so it should exit.

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.