gpt4 book ai didi

将指针转换为 intptr_t 并返回

转载 作者:行者123 更新时间:2023-12-04 11:04:36 27 4
gpt4 key购买 nike

我正在尝试使用 _Generic 制作线程安全的 strerror 函数,该函数与使用 strerror_r 的 XSI 或 GNU 变体无关. XSI 变体返回一个 int 并修改传递给它的缓冲区的内容,而 GNU 版本返回一个 char *

strerror.c

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "strerror.h"

static inline char *strerror_r_gnu(int errnum, char *buf, size_t buflen);
static inline char *strerror_r_xsi(int errnum, char *buf, size_t buflen);

static inline char *strerror_r_gnu(int errnum, char *buf, size_t buflen) {

// FUNKY CASTING TO SILENCE COMPILER WARNING BELOW
return (char *)(intptr_t)strerror_r(errnum, buf, buflen);
}


static inline char *strerror_r_xsi(int errnum, char *buf, size_t buflen) {

// Cast result to int to shut up compiler when using GNU strerror()
int err = (int)strerror_r(errnum, buf, buflen);
if (err) {
snprintf(buf, buflen, "%s error", __func__);

// If your buffer can't fit this error message, you deserve truncation
buf[buflen-1] = '\0';
}
return buf;
}

char *strerror_p(int errnum, char *buf, size_t buflen) {
return _Generic(strerror_r,
int (*)(int, char *, size_t) : strerror_r_xsi(errnum, buf, buflen),
char * (*)(int, char *, size_t) : strerror_r_gnu(errnum, buf, buflen)
);

}

strerror.h

#ifndef STRERROR_H 
#define STRERROR_H

char *strerror_p(int errnum, char *buf, size_t buflen);

#endif // STRERROR_H

ma​​in.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "strerror.h"

int main(void){

const int buflen = 128;
char buf[buflen]; // Flawfinder: ignore
fprintf(stderr, "%s\n", strerror_p(EINVAL, buf, buflen));
return EXIT_SUCCESS;
}

strerror_r_gnu 函数中,如果不转换 strerror_r 的结果,编译器将发出警告 incompatible integer to pointer conversion returning 'int' from a使用 XSI 版本的 strerror_r 编译时结果类型为“char *” 的函数。由于此函数在针对 strerror_r 的 XSI 版本编译时永远不会被调用,所以我不关心类型不匹配,但我希望编译器对此保持沉默。强制转换为 intptr_t,然后转换为 char * 会消除编译器警告。

问题是,当使用返回 char * 的 GNU strerror_r 编译时,将结果转换为 intptr_t 是否明确定义> 并返回到 char *?

最佳答案

The question is, when compiling with the GNU strerror_r that returns a char *, is it well-defined to cast the result to intptr_t and back to char *?

是的。这种操作的安全性几乎就是标准中 intptr_t 的定义:

7.20.1.4 Integer types capable of holding object pointers

1 The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

     intptr_t

(将其与 6.2.5p28 结合起来:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

)

关于将指针转换为 intptr_t 并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com