为什么要调用__pure__函数但不使用答案?

问题描述 投票:0回答:1

对于副项目,我正在阅读一些系统化的源代码。我发现__pure__内部的endswith函数cg_get_root_path的用法令人困惑。具体来说,为什么变量e完全存在于cg_get_root_path函数中?它存储endswith的结果,但似乎从未真正使用过,并且函数endswith没有我看到的副作用。

char *endswith(const char *s, const char *postfix) _pure_;

int cg_get_root_path(char **path) {
        char *p, *e;
        int r;

        assert(path);

        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
        if (r < 0)
                return r;

        e = endswith(p, "/" SPECIAL_INIT_SCOPE);
        if (!e)
                e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
        if (!e)
                e = endswith(p, "/system"); /* even more legacy */
        if (e)
                *e = 0;

        *path = p;
        return 0;
}

char* endswith(const char *s, const char *postfix) {
        size_t sl, pl;

        assert(s);
        assert(postfix);

        sl = strlen(s);
        pl = strlen(postfix);

        if (pl == 0)
                return (char*) s + sl;

        if (sl < pl)
                return NULL;

        if (memcmp(s + sl - pl, postfix, pl) != 0)
                return NULL;

        return (char*) s + sl - pl;
}
c systemd
1个回答
0
投票

最终在一行中使用了e*e = 0写入内存。

© www.soinside.com 2019 - 2024. All rights reserved.