You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "jihuayu (via GitHub)" <gi...@apache.org> on 2024/01/14 04:53:49 UTC

[PR] Add type check before metadata decode [kvrocks]

jihuayu opened a new pull request, #2015:
URL: https://github.com/apache/kvrocks/pull/2015

   In this PR I add type check before metadata decode.
   I also add some test cases about operating with the wrong type
   It fixes #2012


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1451662709


##########
src/storage/redis_metadata.cc:
##########
@@ -372,29 +372,31 @@ rocksdb::Status StreamMetadata::Decode(Slice *input) {
     return s;
   }
 
-  if (input->size() < 8 * 11) {
-    return rocksdb::Status::InvalidArgument(kErrMetadataTooShort);
-  }
+  if (Type() == kRedisStream) {

Review Comment:
   It is already assumed that in `StreamMetadata::Decode` the type must be kRedisStream, which should be checked in other places. So I don't think it is meaningful.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1451662806


##########
tests/gocase/unit/type/types_test.go:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package types
+
+import (
+	"context"
+	"testing"
+
+	"github.com/apache/kvrocks/tests/gocase/util"
+	"github.com/stretchr/testify/require"
+)
+
+func TestTypesError(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	t.Run("Operate with wrong type", func(t *testing.T) {
+		message := "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value"
+		require.NoError(t, rdb.Set(ctx, "a", "hello", 0).Err())
+		require.EqualError(t, rdb.Do(ctx, "XADD", "a", "*", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "LPUSH", "a", 1).Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "HSET", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "ZADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "JSON.SET", "a", "$", "{}").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "BF.ADD", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", 100).Err(), message)

Review Comment:
   I think the test case can pass without these changes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1890860487

   Instead, if you want to fix #2012, I think a better way is to change `Metadata::ParseMetadata`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "git-hulk (via GitHub)" <gi...@apache.org>.
git-hulk merged PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1893217855

   Please retry analysis of this Pull-Request directly on SonarCloud


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1451662806


##########
tests/gocase/unit/type/types_test.go:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package types
+
+import (
+	"context"
+	"testing"
+
+	"github.com/apache/kvrocks/tests/gocase/util"
+	"github.com/stretchr/testify/require"
+)
+
+func TestTypesError(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	t.Run("Operate with wrong type", func(t *testing.T) {
+		message := "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value"
+		require.NoError(t, rdb.Set(ctx, "a", "hello", 0).Err())
+		require.EqualError(t, rdb.Do(ctx, "XADD", "a", "*", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "LPUSH", "a", 1).Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "HSET", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "ZADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "JSON.SET", "a", "$", "{}").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "BF.ADD", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", 100).Err(), message)

Review Comment:
   I think the test case can pass without these changes, regardless of error messages.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1895505849

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015) **Quality Gate passed**  
   The SonarCloud Quality Gate passed, but some issues were introduced.
   
   [7 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   No data about Coverage  
   [0.4% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=2015&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1452168615


##########
src/storage/redis_metadata.cc:
##########
@@ -372,29 +372,31 @@ rocksdb::Status StreamMetadata::Decode(Slice *input) {
     return s;
   }
 
-  if (input->size() < 8 * 11) {
-    return rocksdb::Status::InvalidArgument(kErrMetadataTooShort);
-  }
+  if (Type() == kRedisStream) {

Review Comment:
   I am according to old code, maybe this code is unused now.  https://github.com/apache/kvrocks/blob/8d30e1eb2476f7c42d6c90f5cf112924e1d48ba9/src/storage/redis_metadata.cc#L338-L344
   
   
   > I think a better way is to change `Metadata::ParseMetadata`.
   
   Great, I will move all to `Metadata::ParseMetadata`.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1451662709


##########
src/storage/redis_metadata.cc:
##########
@@ -372,29 +372,31 @@ rocksdb::Status StreamMetadata::Decode(Slice *input) {
     return s;
   }
 
-  if (input->size() < 8 * 11) {
-    return rocksdb::Status::InvalidArgument(kErrMetadataTooShort);
-  }
+  if (Type() == kRedisStream) {

Review Comment:
   It is already assumed that in `StreamMetadata::Decode` the type must be kRedisStream, which is checked in other places. So I don't think it is meaningful.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on code in PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#discussion_r1452172921


##########
tests/gocase/unit/type/types_test.go:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package types
+
+import (
+	"context"
+	"testing"
+
+	"github.com/apache/kvrocks/tests/gocase/util"
+	"github.com/stretchr/testify/require"
+)
+
+func TestTypesError(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	t.Run("Operate with wrong type", func(t *testing.T) {
+		message := "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value"
+		require.NoError(t, rdb.Set(ctx, "a", "hello", 0).Err())
+		require.EqualError(t, rdb.Do(ctx, "XADD", "a", "*", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "LPUSH", "a", 1).Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "HSET", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "ZADD", "a", "1", "2").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "JSON.SET", "a", "$", "{}").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "BF.ADD", "a", "test").Err(), message)
+		require.EqualError(t, rdb.Do(ctx, "SADD", "a", 100).Err(), message)

Review Comment:
   This test case is designed to ensure that the error message is consistent with Redis. 
   Based on my experience, it's difficult for me to associate the problem with inconsistent key types when I see other error messages.
   So I wrote this test case to prevent future users from being confused by meaningless error messages.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1893213966

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015) **Quality Gate passed**  
   Kudos, no new issues were introduced!
   
   [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   No data about Coverage  
   [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=2015&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1890847500

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015) **Quality Gate passed**  
   Kudos, no new issues were introduced!
   
   [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   No data about Coverage  
   [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=2015&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1893217850

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015) **Quality Gate passed**  
   Kudos, no new issues were introduced!
   
   [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   No data about Coverage  
   [0.6% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=2015&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1893224201

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015) **Quality Gate passed**  
   Kudos, no new issues were introduced!
   
   [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2015&resolved=false&inNewCodePeriod=true)  
   No data about Coverage  
   [0.6% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=2015&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2015)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add type check before metadata decode [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on PR #2015:
URL: https://github.com/apache/kvrocks/pull/2015#issuecomment-1893240687

   @PragmaTwice 
   I delay InvalidArgument error check after type match check in `Metadata::ParseMetadata`.
   
   For the tests, I think it is necessary.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org