【问题标题】:How can I cause a "initscr" break with ncurses?如何使用 ncurses 导致“initscr”中断?
【发布时间】:2021-05-13 12:22:59
【问题描述】:

我知道这是一个奇怪的问题,但我想获取提到的“initscr”函数错误by the doc(在 stderr 上获取无效指针和错误消息)来测试包装器是否正常工作。

但我没有找到任何相关信息。我目前正在使用 ncurses 6.2。

经过一番研究,我发现无效指针实际上是一个 NULL,而不仅仅是一个指向任何地方的空指针。

但我无法破坏功能......

如果有人知道如何帮助我解决这个问题,请随时发表评论。

【问题讨论】:

  • the "initscr" error 什么错误? by the doc 什么文档?你能发个链接吗? to break the function什么功能? “破”具体是什么意思?你想把一个函数分成几块?你想从函数中return
  • 对不起,我已经编辑了这个问题。还好吗?
  • Copyright © 1997 The Open Group 版本有点旧 :p 所以你想“制造”initscr 错误?检查它在 ncurses 中的来源,看看你能做什么。 man initscr 看起来更好。另外我认为你提到的文档很清楚:Upon successful completion, initscr() returns a pointer to stdscr. Otherwise, it does not return。如果出现错误 initscr(),可能会在 UNIXv2 上调用 abort()。那么getting invalid pointer and an error message on stderr 你指的是什么?
  • 手册页遵循行为 - If errors occur, initscr writes an appropriate error message to standard error and exits - “退出”,如调用 exit()。是的 - 用行话来说,“无效指针”可以理解为NULL,但“无效指针”确实是任何指向任何地方的指针值,不仅是NULL
  • 好的 - github.com/mirror/ncurses/blob/master/ncurses/base/… 这里是 initscr。你可以“打破”动态分配——写你自己的malloc并在initscr中返回NULL,这样strdup就会失败。

标签: c linux reverse-engineering ncurses


【解决方案1】:

以下程序:

#include <curses.h>
#include <malloc.h>
#include <stdlib.h>

bool my_malloc_disabled;
void *malloc(size_t size) {
    if (my_malloc_disabled) {
        return NULL;
    }
    void *__libc_malloc(size_t);
    return __libc_malloc(size);
}
int main() {
    my_malloc_disabled = 1;
    initscr();
}

做:

$ gcc file.c -lcurses
$ ./a.out
Error opening allocating $TERM.

【讨论】:

  • 谢谢!我是 C 新手,我不知道我们可以禁用 malloc ! :)
  • 此代码特定于glibc。还有hooks for malloc,它们仍然可以工作并且很好,但被认为已弃用(在手册页中提到)。 __libc_* 是所有(大多数)标准函数的 internal glibc prefix,因此您可以根据需要覆盖它们。
猜你喜欢
  • 2023-03-30
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2016-11-09
  • 2017-06-29
相关资源
最近更新 更多