简体   繁体   English

在C中验证整数参数

[英]validation of an integer parameter in C

I was told one of the best practice with Functions with parameters is to perform parameter validation. 有人告诉我带参数的函数的最佳实践之一是执行参数验证。
So for string checking null or empty and so on. 因此,对于字符串检查为null或为空等等。

I am creating a List and for that creating a node which accepts integer value. 我正在创建一个列表,并为此创建了一个接受整数值的节点。 I am checking the value against INT_MIN and INT_MAX but from what I read int value is always going to be between this. 我正在对照INT_MININT_MAX检查该值,但是从我读到的值INT_MIN ,int值总是介于此之间。 If I pass a long long val, in the called function with signature node* create(int val) - value gets converted to int range and falls within. 如果我传递了一个long long val,则在带有signature node* create(int val)的调用函数中signature node* create(int val) -值将转换为int范围并落入其中。

Question : if val of long is being passed, is there a way to detect it and not let the node be created ? 问题:如果传递了long of val,有没有办法检测到它并且不让该节点创建? Currently it gets created 目前已创建

Question : for a standard int what else checking of parameter do I need to perform to write a robust code ? 问题:对于标准int,编写健壮的代码还需要执行哪些参数检查?

My code Snippet 我的代码段

long long vall = 88888888888888; 
create(vall);     // for such case I don't want node to be created

node* create(int val)
{
    if (val <= INT_MIN || val >= INT_MAX)
    {
        printf(" values are out of range");
    }
    node *pnode = (node *)malloc(sizeof(node));
    if(pnode == NULL)
    {
        printf(" node creation failed \n");
    }

    pnode->pnext = NULL;
    pnode->val = val;

    return pnode;
}

if val of long is being passed, is there a way to detect it and not let the node be created ? 如果已通过val of long ,是否有一种方法可以检测到它并且不让该节点创建?

Change function signature to long or long long , etc. 将功能签名更改为longlong long等。

// declare/define function first
node* create(long long val);

long long vall = 88888888888888; 
create(vall);     // for such case I don't want node to be created

node* create(long long val) {
    // if (val <= INT_MIN || val >= INT_MAX) {
    if (val < INT_MIN || val > INT_MAX) {
        fprintf(stderr, " %lld out of int range\n", val);

        return NULL;
        // Return or exit, do not continue with the following code 
    }

    // node *pnode = (node *)malloc(sizeof(node));
    // Suggest alternative
    node *pnode = malloc(sizeof *pnode);
    ...
    // Adding an explicit cast useful to quiet pedantic warnings
    pnode->val = (int) val;
    ...

Note: With original code, many compilers with all warnings enabled will warn about the below (without knowing the value of vall ) as a narrowing of range. 注意:对于原始代码,许多启用了所有警告的编译器都会警告以下内容(不知道vall的值),因为它们会缩小范围。

node* create(int val)
long long vall = ...; 
create(vall);

for a standard int what else checking of parameter do I need to perform to write a robust code ? 对于标准int ,还需要执行什么参数检查才能编写健壮的代码?

Nothing, unless the function can only use a sub-range of int . 没什么,除非函数只能使用int的子范围。 All int values are in the range [INT_MIN ... IN_MAX] . 所有int值都在[INT_MIN ... IN_MAX]范围内。 @Eugene Sh. @尤金Sh。

You may create one more function with the same name but different arguments list: 您可以再创建一个名称相同但参数列表不同的函数:

node* create(long long val);

If you pass a value of type long long , this function will run, not node* create(int val); 如果传递的值类型为long long ,则此函数将运行,而不是node* create(int val); . So, you can handle the case, when you pass long as a parameter, through creating another function. 因此,当您将long作为参数传递时,可以通过创建另一个函数来处理这种情况。

As for your second questinon, I should say, that you don't need to check your int value against INT_MIN and INT_MAX boundaries, because it will never be out of these bounds. 关于第二个问题,我应该说,您不需要对照INT_MININT_MAX边界检查int值,因为它永远不会超出这些范围。 INT_MAX and INT_MIN are ACTUAL BOUNDARIES OF int TYPE ITSELF. INT_MAXINT_MINint类型为INT_MIN的实际INT_MIN

Check your int value considering your needs (needs of your function). 考虑您的需求(您的功能需求),检查您的int值。

You need to check passed strings for NULL , because if you try to access such string, your program will collapse. 您需要检查传递的字符串是否为NULL ,因为如果尝试访问此类字符串,则程序将崩溃。 But when you pass an int value, it will have an assigned value anyway. 但是,当您传递一个int值时, 无论如何它将为它分配一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM