property_get的编译报错问题

在使用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++里的新特性功能真是让人吃惊!

扫描二维码关注公众号,回复: 8552688 查看本文章

 

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

 

发布了336 篇原创文章 · 获赞 13 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/aaajj/article/details/88829844