property_get的编译报错问题

74 篇文章 1 订阅

在使用property_get的时候,竟然发现有个坑,

       char value[64] = {0};//PROPERTY_VALUE_MAX

           property_get("debug.slamlog", value, "0");

编译的时候会出错,

 

frameworks/native/cmds/tam/tamService.cpp:253:6: error: call to unavailable function 'property_get': property_get() called with too small of a buffer

            property_get("debug.tamlog", value, "0");

给定的字符串长度不是PROPERTY_VALUE_MAX导致的报错,

但是,编译的时候竟然会检查这个?

确实如此。

不得不说,C++里的新特性功能真是让人吃惊!

 

system/core/include/cutils/properties.h

里面的定义造成的

 

118#define __property_get_err_str "property_get() called with too small of a buffer"

 

129#pragma clang diagnostic push
130#pragma clang diagnostic ignored "-Wgcc-compat"
131__BIONIC_ERROR_FUNCTION_VISIBILITY
132int property_get(const char *key, char *value, const char *default_value)
133        __overloadable
134        __enable_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
135                    __bos(value) < PROPERTY_VALUE_MAX, __property_get_err_str)
136        __errorattr(__property_get_err_str)
137        __attribute__((used));
138#pragma clang diagnostic pop

 

 

__attribute__造成的

 

参考资料:

https://www.jianshu.com/p/c43579ff7d99

 

这个属性只能用在 C 函数上,可以用来实现参数的静态检查:

static void printValidAge(int age)

__attribute__((enable_if(age > 0 && age < 120, "你丫火星人?"))) {

printf("%d", age);

}

它表示调用这个函数时必须满足age > 0 && age <120 才被允许,于是乎:

printValidAge(26); // √

printValidAge(150); // <--- Compile Error

printValidAge(-1); // <--- Compile Error

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值