Linux内核指针判断

内核指针判断

https://blog.csdn.net/jasonchen_gbd/article/details/44968395
https://blog.csdn.net/xxu0123456789/article/details/6339625

#ifndef _LINUX_ERR_H
#define _LINUX_ERR_H

#include <linux/compiler.h>

#include <asm/errno.h>

/*
 * Kernel pointers have redundant information, so we can use a
 * scheme where we can return either an error code or a dentry
 * pointer with the same return value.
 *
 * This should be a per-architecture thing, to allow different
 * error and pointer decisions.
 */
#define MAX_ERRNO   4095
#ifndef __ASSEMBLY__
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
static inline void * __must_check ERR_PTR(long error)
{
    return (void *) error;
}
static inline long __must_check PTR_ERR(__force const void *ptr)
{
    return (long) ptr;
}
static inline long __must_check IS_ERR(__force const void *ptr)
{
    return IS_ERR_VALUE((unsigned long)ptr);
}
static inline long __must_check IS_ERR_OR_NULL(__force const void *ptr)
{
    return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}
/**
 * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
 * @ptr: The pointer to cast.
 *
 * Explicitly cast an error-valued pointer to another pointer type in such a
 * way as to make it clear that's what's going on.
 */
static inline void * __must_check ERR_CAST(__force const void *ptr)
{
    /* cast away the const */
    return (void *) ptr;
}
static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
{
    if (IS_ERR(ptr))
        return PTR_ERR(ptr);
    else
        return 0;
}
/* Deprecated */
#define PTR_RET(p) PTR_ERR_OR_ZERO(p)
#endif
#endif /* _LINUX_ERR_H */

task = kthread_run(xx_task, 0, “name”);
if (!IS_ERR_OR_NULL(task))
task_running = 1;
else
task = NULL;
增加了内核指针保护,留给内核的空间是3G–>4G的虚拟地址,并且还预留了一个基本页内存4k的地址空间(这部分还不够页4k)对应是一些错误码的表达。
判断错误指针的方式就是IS_ERR_VALUE。IS_ERR_VALUE就是判断指针是否对应是错误码对应的指针表达范围。
如果没有添加此容错的措施,操作无效地址的;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值