0

I'm having a strange thing happen to me. I'm calling printf() THEN read() in my program, but the read() happens first, and the printf() afterward? I've cut this down to the smallest possible code which shows the problem. If you type something in, it does pick everything up correctly.

.data

.balign 4
message1:       .asciz "Please type a string: "

.balign 4
input_string:
    .asciz ""
    .zero 80

return: .word 0

.text
.global main

main:
    push {lr}

    ldr r0, =message1
    bl printf

    ldr r1, =input_string
    mov r3, #80
    mov r0, #0

    bl read
    mov r0, r1
    bl printf

    pop {lr}
    bx lr

.end


.global printf
.global read
Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • 1
    Perhaps the output from the first `printf` is buffered. You may need to call a function to flush that buffer. – 1201ProgramAlarm Sep 27 '20 at 22:48
  • 3
    Your `message1` is not terminated with a line feed and standard output is typically line buffered. Try flushing the buffer or using `fgets` instead as that might do it for you. PS: it's a bad idea to pass user input to `printf` without a format string. – Jester Sep 27 '20 at 22:49
  • 3
    Also `read` might very well destroy your `r1` so relying on that being unchanged isn't very robust either. – Jester Sep 27 '20 at 23:12
  • 1
    Also, this has a format-string vulnerability. The user can crash your program or overwrite memory by entering a string like `%n` or `%*0n`. Or leak ASLR address information with `%*0p`. If you want to print user input, use `printf("%s", buffer)`, or `fputs`, or since you know the length from the `read` return value, `fwrite` or just `write`. – Peter Cordes Sep 28 '20 at 00:21
  • I have added a terminator to the string, and now it seems to intermittently pause to get the string, or just run past to end of program without pausing for input. So how do I flush the buffer? Or can someone show me an example using fgets? Although I assume I'd have the same problem with fgets with the buffer not being flushed. – Rick Dearman Sep 28 '20 at 09:24

0 Answers0