在php类方法(C)中使最后一个数组参数可选

时间:2012-08-25 21:50:01

标签: php c arrays php-extension php-internals

我在C中创建一个PHP扩展来访问SPI接口。到目前为止,我已经完成了所有工作:php_spi on Github

但是,我似乎无法使构造函数中的$ options参数成为可选项。我的工作代码是这样的:

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lla", &bus, &chipselect, &options) == FAILURE) {
    return;
}

_this_zval = getThis();
_this_ce = Z_OBJCE_P(_this_zval);

options_hash = HASH_OF(options);

char device[32];
sprintf(device, "/dev/spidev%d.%d", bus, chipselect);

// If the device doesn't exists, error!
if(access(device, F_OK) == -1) {
    char error[128];
    sprintf(error, "The device %s does not exist", device);
    php_error(E_ERROR, error);
}

// If we can't open it, error!
long fd = open(device, O_RDWR);
if (fd < 0) {
    char error[128];
    sprintf(error, "Could not open %s for read/write operations, are you running as root?", device);
    php_error(E_ERROR, error);
}

// Set the file descriptor as a class property
zend_update_property_long(_this_ce, _this_zval, "device", 6, fd TSRMLS_DC);

// Default property values
uint8_t mode = SPI_MODE_0;
uint8_t bits = 8;
uint32_t speed = 500000;
uint16_t delay = 0;

// Loop through the options array
zval **data;
for(zend_hash_internal_pointer_reset(options_hash);
    zend_hash_get_current_data(options_hash, (void **)&data) == SUCCESS;
    zend_hash_move_forward(options_hash)) {

    char *key;
    int len;
    long index;
    long value = Z_LVAL_PP(data);

    if(zend_hash_get_current_key_ex(options_hash, &key, &len, &index, 1, NULL) == HASH_KEY_IS_STRING) {
        // Assign the value accordingly
        if(strncmp("mode", key, len) == 0) {
            switch(value) {
                case SPI_MODE_1:
                    mode = SPI_MODE_1;
                    break;
                case SPI_MODE_2:
                    mode = SPI_MODE_2;
                    break;
                case SPI_MODE_3:
                    mode = SPI_MODE_3;
                    break;
                default:
                    mode = SPI_MODE_0;
                    break;
            }
        }
        else if(strncmp("bits", key, len) == 0) {
            bits = value;
        }
        else if(strncmp("speed", key, len) == 0) {
            speed = value;
        }
        else if(strncmp("delay", key, len) == 0) {
            delay = value;
        }
    }
}

但是,如果我按照所有文档的建议,我可以找到并在l和a之间添加一个管道,如下所示:

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|a", &bus, &chipselect, &options) == FAILURE) {

然后我的扩展无声地失败了 - 有人能给我任何建议吗?

1 个答案:

答案 0 :(得分:4)

假设optionszval*,如果您执行此操作:

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|a", &bus, &chipselect, &options) == FAILURE) {
    return;
}

...如果未传递options(即省略第三个可选参数),则不会初始化或修改options。之后,你这样做:

options_hash = HASH_OF(options);

因此,您使用的是未初始化的指针或NULL指针,这是未定义的行为。这很可能导致分段错误,导致PHP脚本失败。

你应该做的是:

zval* options = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|a", &bus, &chipselect, &options) == FAILURE) {
    return;
}

// ...

if (options != NULL) {
    options_hash = HASH_OF(options);
}

...并处理options(和options_hash)的每个实例,其条件是检查它是否为NULL