什么&QUOT *((字符*) - 1)='x'的;" code是什么意思? [英] What does "*((char*)-1) = 'x';" code mean?

查看:160
本文介绍了什么&QUOT *((字符*) - 1)='x'的;" code是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读书的Redis源$ C ​​$ C的一个问题,谁能告诉我什么是使用在调试 _redisAssert 函数的最后声明.C 的:

  *((的char *) -  1)='X';


解决方案

更新

我发现 debug.c 行在OP提到的,我们可以从上面两行此code看到:

  redisLog(REDIS_WARNING(强制SIGSEGV打印错误报告)。);

和同一code可以在 _redisPanic 发现一样,所以它看起来像自己的方式来强制 SIGSEGV 当断言失败或出现恐慌。

原始

这看起来像一个调试工具,我们可以从这个文件请参见 Redis的调试指南和相关部门表示,


  

Redis的有一个命令,模拟分段故障(换句话说,一个坏的崩溃)使用DEBUG命令段错误(不使用它,当然对一个真正的生产实例)。所以,我会用这个命令崩溃我的实例展现在广发行方会发生什么:


和显示了这个GDB输出:

 (GDB)继续
 继续。 节目接收信号EXC_BAD_ACCESS,无法访问内存。
 原因:KERN_INVALID_ADDRESS地址:0xffffffffffffffff
 debugCommand(C = 0x7ffc32005000)在debug.c:220
 220 *((字符*) - 1)='×';
             ^^^^^^^^^^^^^^^^^^^

在做什么,是铸造 1 的char * 的,然后在其上进行间接和分配 X'来该内存位置。作为ALK链接线程((无效*)-1)的有效地址<? / A>表示,在大多数系统上它不会是有效的访问,更不用说分配一个值。这将产生上最先进的操作系统上分段故障

这是未定义行为,并为被走了过去在线程<一个href=\"http://stackoverflow.com/questions/18986351/what-is-the-simplest-standard-conform-way-to-produce-a-segfault-in-c/18986406#18986406\">What是最简单的标准一致的方式在C中产生一个段错误?的,不能依靠。编译器变得越来越聪明,有一些著名的例子,其中编译器是聪明意外利用未定义行为和坏的方式。

I had a problem in reading redis source code, can anyone tell me what is the use of the last statement in the _redisAssert function in debug.c:

*((char*)-1) = 'x';

解决方案

Update

I found the line in debug.c mentioned in the OP and we can see from two lines above this code:

redisLog(REDIS_WARNING,"(forcing SIGSEGV to print the bug report.)");

and the same code can be found in _redisPanic as well, so it looks like their way to force a SIGSEGV when an assertion fails or there is a panic.

Original

This looks like a debugging tool, we can see from this document Redis debugging guide and relevant section says:

Redis has a command to simulate a segmentation fault (in other words a bad crash) using the DEBUG SEGFAULT command (don't use it against a real production instance of course ;). So I'll use this command to crash my instance to show what happens in the GDB side:

and shows this gdb output:

 (gdb) continue
 Continuing.

 Program received signal EXC_BAD_ACCESS, Could not access memory.
 Reason: KERN_INVALID_ADDRESS at address: 0xffffffffffffffff
 debugCommand (c=0x7ffc32005000) at debug.c:220
 220         *((char*)-1) = 'x';
             ^^^^^^^^^^^^^^^^^^^

What it is doing is casting -1 to a char * and then performing indirection on it and assigning 'x' to that memory location. As the thread that alk linked Is ((void *) -1) a valid address? says on most systems it will not be valid to access, let alone assign a value to. This will generate a segmentation fault on most modern operating systems.

This is undefined behavior and as was went over in the thread What is the simplest standard conform way to produce a Segfault in C? it can not be relied on. Compilers are getting smarter and there are some famous examples where the compiler is smart about exploiting undefined behavior in unexpected and bad ways.

这篇关于什么&QUOT *((字符*) - 1)='x'的;&QUOT; code是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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