PHP 扩展 : why int var changes to 0?

标签 php c php-extension

我正在 C 上开发一些 PHP 扩展。我在 C 代码的顶部全局定义了几个函数和几个变量(char 和 int)。在 php 端,我调用第一个函数并为 ssh_port 参数传递一些值 - 22。此值设置为全局变量。然后我调用第二个函数。但是当我调用第三个函数时——ssh_port 变量突然变为 0 值?为什么 ?代码如下:

#include <php.h>
#include "super_ssh.h"

#include <libssh2.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>

#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifndef INADDR_NONE
#define INADDR_NONE (in_addr_t)-1
#endif

    char *ssh_ip, *ssh_username, *ssh_password, *remote_host, *last_error = "";
    size_t ssh_ip_len, ssh_username_len, ssh_password_len, remote_host_len;

    int ssh_port = 0, remote_port = 0, local_port = 0, to_close_tunnel = 0, thread_is_runnning = 0, is_authenticated = 0;

    PHP_FUNCTION(super_ssh_connect) {
      if (zend_parse_parameters(
        ZEND_NUM_ARGS(),
        "sl",
        &ssh_ip,
        &ssh_ip_len,
        &ssh_port
      ) == FAILURE) {
          return;
      }

      RETURN_LONG(ssh_port);

    }

    PHP_FUNCTION(super_ssh_authenticate) {
      if (zend_parse_parameters(
        ZEND_NUM_ARGS(),
        "ss",
        &ssh_username,
        &ssh_username_len,
        &ssh_password,
        &ssh_password_len
      ) == FAILURE) {
          return;
      }

      RETURN_LONG(ssh_port);
    }

    PHP_FUNCTION(super_ssh_open_tunnel) {

      if (zend_parse_parameters(
        ZEND_NUM_ARGS(),
        "sll",
        &remote_host,
        &remote_host_len,
        &remote_port,
        &local_port
      ) == FAILURE) {
          return;
      }

      RETURN_LONG(ssh_port);

    }





    ZEND_BEGIN_ARG_INFO_EX(arginfo_super_ssh_connect, 0, 0, 2)
      ZEND_ARG_INFO(0, ssh_ip)
      ZEND_ARG_INFO(0, ssh_port)
    ZEND_END_ARG_INFO()

    ZEND_BEGIN_ARG_INFO_EX(arginfo_super_ssh_authenticate, 0, 0, 2)
        ZEND_ARG_INFO(0, ssh_username)
      ZEND_ARG_INFO(0, ssh_password)
    ZEND_END_ARG_INFO()

    ZEND_BEGIN_ARG_INFO_EX(arginfo_super_ssh_open_tunnel, 0, 0, 3)
      ZEND_ARG_INFO(0, remote_host)
      ZEND_ARG_INFO(0, remote_port)
      ZEND_ARG_INFO(0, local_port)
    ZEND_END_ARG_INFO()

    zend_function_entry super_ssh_functions[] = {
      PHP_FE(super_ssh_connect, arginfo_super_ssh_connect)
      PHP_FE(super_ssh_authenticate, arginfo_super_ssh_authenticate)
      PHP_FE(super_ssh_open_tunnel, arginfo_super_ssh_open_tunnel)
      PHP_FE_END
    };

    zend_module_entry super_ssh_module_entry = {
      STANDARD_MODULE_HEADER,
      PHP_SUPER_SSH_EXTNAME,
      super_ssh_functions,
      NULL,
      PHP_MSHUTDOWN(super_ssh),
      NULL,
      NULL,
      NULL,
      PHP_SUPER_SSH_VERSION,
      STANDARD_MODULE_PROPERTIES,
    };

    ZEND_GET_MODULE(super_ssh);

以及PHP端的代码:

<?php

var_dump(super_ssh_connect("234.43.23.3", 22));
var_dump(super_ssh_authenticate("dfds", "mofsdfdsnitor"));
var_dump(super_ssh_open_tunnel("server", 993, 4444));

实际结果是:

整数(22)整数(22)整数(0)

预期结果是:

整数(22)整数(22)整数(22)

所以问题是为什么 ssh_port 变量的值为 0?我没有在任何地方设置它!实在看不懂:(

有趣的是,这个问题实际上是针对 apache php 的。所以当我从浏览器运行脚本时会发生这种情况。 (apache2 + php)

如果我尝试对 PHP CLI 进行相同的编译,它会按预期工作,输出为:

整数(22)整数(22)整数(22)

可能与垃圾收集器有某种联系?或者,也许您可​​以为我的代码建议更好的方式来处理变量?

我的环境:

Ubuntu 18.04.1 LTS,apache2 + PHP 版本 7.2.17-1,PHP CLI 为 PHP 7.2.2 (cli)(构建时间:2019 年 4 月 29 日 09:57:40)(ZTS DEBUG)

我注意到有问题的代码,如果我将以下代码注释为 frpm 第三个 super_ssh_open_tunnel 函数 - 它可以工作。

注释这段代码使它工作,没有这段代码我在 ssh_port var 中仍然有 22

 if (zend_parse_parameters(
        ZEND_NUM_ARGS(),
        "sll",
        &remote_host,
        &remote_host_len,
        &remote_port,
        &local_port
      ) == FAILURE) {
          return;
      }

最佳答案

实际上,我认为你有一个未定义的行为。

首先 PHP_FUNCTION 被扩展为 void zif_FUNCNAME(zend_execute_data *execute_data, zval *return_value)

根据 C Standard :

6.8.6.4 The return statement

Constraints

  1. A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

6.9.1 Function definitions

  1. If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

因此,在您的情况下,zend_parse_parameters 返回 FAILURE,然后将其转换为“在到达结束前结束 {”,这就是为什么ssh_port 永远不会返回。

zend_parse_parameters 失败的原因是其中一个参数未初始化为不同于 0

关于PHP 扩展 : why int var changes to 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936245/

相关文章:

php - 使用 Angular 和 php 插入、显示和删除数据

C程序从数组中打印出LCD数字

c - 类型转换的未定义行为?

php - 在 php 中检索扩展版本

c++ - PHP-CPP 新扩展适用于 cli 'php' 命令,但不适用于浏览器

php - 如何将数组从 View 传递到 Controller

php - 如何在不减慢响应速度的情况下调用微服务?

php - Python 等效于 PHP array_column

c - 在 C 中刷新标准输入缓冲区