Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare of statements with $options set to null causes Exception #108

Open
CharlemagneLasse opened this issue Dec 2, 2024 · 0 comments
Open

Comments

@CharlemagneLasse
Copy link

CharlemagneLasse commented Dec 2, 2024

The PHP_METHOD(DefaultSession, prepare) { is telling me to set $options to null. But when I am doing this then there will be a cassandra exception:

See:

  if (options) {
    if (Z_TYPE_P(options) != IS_ARRAY &&
        (Z_TYPE_P(options) != IS_OBJECT ||
         !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce))) {
      INVALID_ARGUMENT(
          options, "an instance of " PHP_DRIVER_NAMESPACE "\\ExecutionOptions or an array or null");
    }

    if (Z_TYPE_P(options) == IS_OBJECT) {
      opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options);
    } else {
      if (php_driver_execution_options_build_local_from_array(&local_opts, options) == FAILURE) {
        return;
      }
      opts = &local_opts;
    }
    timeout = &opts->timeout;
  }

Example code:

$session->prepare('SELECT * from foobar;', null);

Possible fix:

diff --git a/src/DefaultSession.cpp b/src/DefaultSession.cpp
index 5976afa3..05a391e2 100644
--- a/src/DefaultSession.cpp
+++ b/src/DefaultSession.cpp
@@ -535,6 +535,10 @@ PHP_METHOD(DefaultSession, execute) {
   page_size = self->default_page_size;
   timeout = &self->default_timeout;
 
+  if (options && Z_TYPE_P(options) == IS_NULL) {
+    options = NULL;
+  }
+
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
         (Z_TYPE_P(options) != IS_OBJECT ||
@@ -678,6 +682,10 @@ PHP_METHOD(DefaultSession, executeAsync) {
   consistency = static_cast<CassConsistency>(self->default_consistency);
   page_size = self->default_page_size;
 
+  if (options && Z_TYPE_P(options) == IS_NULL) {
+    options = NULL;
+  }
+
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
         (Z_TYPE_P(options) != IS_OBJECT ||
@@ -767,6 +775,10 @@ PHP_METHOD(DefaultSession, prepare) {
 
   self = PHP_DRIVER_GET_SESSION(getThis());
 
+  if (options && Z_TYPE_P(options) == IS_NULL) {
+    options = NULL;
+  }
+
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
         (Z_TYPE_P(options) != IS_OBJECT ||

or:

--- a/src/DefaultSession.cpp
+++ b/src/DefaultSession.cpp
@@ -537,6 +537,7 @@ PHP_METHOD(DefaultSession, execute) {
 
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
+        Z_TYPE_P(options) != IS_NULL &&
         (Z_TYPE_P(options) != IS_OBJECT ||
          !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce))) {
       INVALID_ARGUMENT(
@@ -545,13 +546,15 @@ PHP_METHOD(DefaultSession, execute) {
 
     if (Z_TYPE_P(options) == IS_OBJECT) {
       opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options);
-    } else {
+    } else if (Z_TYPE_P(options) == IS_ARRAY) {
       if (php_driver_execution_options_build_local_from_array(&local_opts, options) == FAILURE) {
         return;
       }
       opts = &local_opts;
     }
+  }
 
+  if (opts) {
     if (!Z_ISUNDEF(opts->arguments)) arguments = Z_ARRVAL(opts->arguments);
 
     if (opts->consistency >= 0) consistency = (CassConsistency)opts->consistency;
@@ -680,6 +683,7 @@ PHP_METHOD(DefaultSession, executeAsync) {
 
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
+        Z_TYPE_P(options) != IS_NULL &&
         (Z_TYPE_P(options) != IS_OBJECT ||
          !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce))) {
       INVALID_ARGUMENT(
@@ -688,13 +692,15 @@ PHP_METHOD(DefaultSession, executeAsync) {
 
     if (Z_TYPE_P(options) == IS_OBJECT) {
       opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options);
-    } else {
+    } else if (Z_TYPE_P(options) == IS_ARRAY) {
       if (php_driver_execution_options_build_local_from_array(&local_opts, options) == FAILURE) {
         return;
       }
       opts = &local_opts;
     }
+  }
 
+  if (opts) {
     if (!Z_ISUNDEF(opts->arguments)) arguments = Z_ARRVAL(opts->arguments);
 
     if (opts->consistency >= 0) consistency = (CassConsistency)opts->consistency;
@@ -769,6 +775,7 @@ PHP_METHOD(DefaultSession, prepare) {
 
   if (options) {
     if (Z_TYPE_P(options) != IS_ARRAY &&
+        Z_TYPE_P(options) != IS_NULL &&
         (Z_TYPE_P(options) != IS_OBJECT ||
          !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce))) {
       INVALID_ARGUMENT(
@@ -777,12 +784,15 @@ PHP_METHOD(DefaultSession, prepare) {
 
     if (Z_TYPE_P(options) == IS_OBJECT) {
       opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options);
-    } else {
+    } else if (Z_TYPE_P(options) == IS_ARRAY) {
       if (php_driver_execution_options_build_local_from_array(&local_opts, options) == FAILURE) {
         return;
       }
       opts = &local_opts;
     }
+  }
+
+  if (opts) {
     timeout = &opts->timeout;
   }
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant