空终止字符串,打开文件进行读取 [英] Null-terminated string, opening file for reading

查看:48
本文介绍了空终止字符串,打开文件进行读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sys_open 系统调用,并且我获得了用于读取的文件描述符.这是我的程序:

I'm experimenting with sys_open syscall and I get file descriptor for reading. Here is my program:

SYS_exit equ 0x3C

SYS_open equ 0x02
O_RDONLY equ 0x00
O_WRONLY equ 0x01
O_RDWR equ 0x02

section .text
    global _start

_start:
    mov eax, SYS_open
    mov rdi, file_name
    mov rsi, O_RDONLY
    mov rdx, 0x00
    syscall

    mov eax, SYS_exit
    mov rdi, 0x00

    syscall

section .data
    file_name: db '/path/to/test\0'

所以当我运行 strace ./bin 我得到了输出:

So when I ran strace ./bin I got the output:

open("/path/to/test\\0", O_RDONLY) = -1 ENOENT (No such file or directory)
exit(0)   

删除空终端后它似乎工作正常:

After deleting the null-terminal it seemed to work fine:

open("/path/to/test", O_RDONLY) = 3
exit(0)                                 = ?

我很好奇汇编器如何知道我的字符串的长度.二进制中数据部分的内容是这样的:

I'm curious about how does the assembler knows the lenght of my string. The content of data section in the binary is this:

Contents of section .data:
 6000d8 2f706174 682f746f 2f746573 74        /path/to/test

我希望读取字符串直到到达空终止符.它是如何工作的?

I expected the string is read till reaching the null-terminator. How does it work?

推荐答案

问题在于您定义以下数据的方式:

The problem is in the way you defined the following data:

section .data
    file_name: db '/path/to/test\0'

缺少尾随的 NUL 字符,因为字符串中的 \0 对应于字符 \0,它应该被定义为:

The trailing NUL character is missing, since the \0 inside the string corresponds to the characters \ and 0, it should be defined instead as:

section .data
    file_name: db '/path/to/test', 0

这篇关于空终止字符串,打开文件进行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆