glibc中xdr的一个bug

本人在64位linux服务器上(centos7),发现xdr_u_long这个函数有个bug,就是数字的范围如果超过unsigned int的最大值(4294967295)时,xdr_u_long失败。

这个场景主要用在unix时间戳上面,比如一款软件,设置有效期为100年。即失效时间为2124年10月26日,对应的unix时间戳大概为4885545600。而xdr_u_long在编码数字4885545600时,会报错。

经过测试,xdr_u_long所能编码的最大值为4294967295,此时间戳对应的时间大概是2106年。

但是xdr_u_long的函数

extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;

这里面的u_long是unsigned long int;本人是64位的gcc编译器,long类型是8个字节,所能表示的数字范围远远大于4294967295。但是4294967296代入这个函数就会报错,对应的c语言代码为:

c 复制代码
#include <stdio.h>
#include <rpc/xdr.h>

int main()
{
    XDR xdr;
    char buff[120];
    unsigned long time = 4294967296;
    xdrmem_create(&xdr,buff,120,XDR_ENCODE);
    if(!xdr_u_long(&xdr, &time))
    {
        printf("xdr encode failed\n");
        return -1;
    }
    return 0;
}

如果要编码大于4294967295不报错,可以使用函数xdr_uint64_t。

而uint64_t和u_long的类型是一致的,都是unsigned long int。

所以说xdr_u_long在表示范围大于4294967295时,存在bug。

相关推荐
orion575 小时前
Missing Semester Class1:course overview and introduction of shell
linux
用户1204872216111 小时前
Linux驱动编译与加载
linux·嵌入式
用户8055336980317 小时前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户8055336980317 小时前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房1 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia2 天前
linux curl命令详解_curl详解
linux
扛枪的书生2 天前
Linux 网络管理器用法速查
linux
顺风尿一寸2 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode3 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫3 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux