【问题标题】:Linux module Case label does not reduce to integer constantLinux模块案例标签不会减少为整数常量
【发布时间】:2021-07-14 09:35:44
【问题描述】:

我正在尝试使用结构 A 的大小

#define IOCTL_XYZ _IOWR(MAJOR_NUMBER, 10, A)

用于定义 IOCTL 但出现错误

error: case label does not reduce to an integer constant
         case IOCTL_XYZ:

当 IOCTL_XYZ 在 ioctl 处理程序中与 case 语句一起使用时,它不是整数常量。

#define ARRAY_SIZE1    4
#define ARRAY_SIZE2    4
#define ARRAY_SIZE3    8
#define ARRAY_SIZE4    4
#define ARRAY_SIZE5    6
    
typedef struct
{
    union
    {
        struct
        {
            uint64_t item1        :   4;
            uint64_t item2        :   4;
            uint64_t item3        :   8;
            uint64_t item4        :   48;
        };
        
        uint64_t combined;
    };
        
    union
    {
        uint64 array1[ARRAY_SIZE4];
        uint64 array2[ARRAY_SIZE5];
        uint64 array3[ARRAY_SIZE4];
    };
} B;
    
typedef struct
{
    uint64 element1;
    uint64 element2[ARRAY_SIZE1];        
    B element3[ARRAY_SIZE2];
    uint64 element4[ARRAY_SIZE3];
    
} A;

我还有其他 ioctl 使用这样的结构,例如:

typedef struct B
{
    uint32     item1;
    uint32     item2;
} B;
 
typedef struct C
{
    uint32 item1;
} C;

typedef struct
{
    uint32 item1;
    bool   item2;
    union
    {
        B  element1;
        C  element2;
    };
    uint32 item3;
} A;

并且不会出现此错误。 是由于结构数组吗?但是数组有固定的大小吗?在这种情况下,编译器如何在编译时不知道大小?

【问题讨论】:

  • 代码中有ARRAY_SIZE6,但定义中没有。这是发布错误还是可能是原因? sizeof(A) 不会被知道,因为它可能将 ARRAY_SIZE6 视为整数变量,而 IOWR() 表达式不是常量。
  • 编辑问题以提供minimal reproducible example
  • @artlessnoise:抱歉,这是发帖错误。我已经更新了问题。
  • 我试图通过填补空白并替换无效的类型名称来重现它,但它对我有用。我猜你忘了#include 什么?

标签: c linux-kernel switch-statement sizeof


【解决方案1】:

此错误的原因之一是数组大小对于 IOCTL 来说太大,这导致总大小超过了允许的 16K 限制。 我已经在问题中编辑了这个示例代码的数组大小,所以它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多