aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordan sinclair <dj2@everburning.com>2019-02-19 12:51:20 -0500
committerGitHub <noreply@github.com>2019-02-19 12:51:20 -0500
commitf2a13b59ad5cbff97c7a672e59766291a2eddaee (patch)
treef91b4978f77879920ee607e689713a3d365413e8 /src
parent735243802dc66120ca9a536c8059f42db2ac9b72 (diff)
downloadamber-f2a13b59ad5cbff97c7a672e59766291a2eddaee.tar.gz
Fixup sample app buffer parsing (#300)
This CL fixes the -B flag to default to 0:0 if not provided. It also fixes the code to use : as the separator instead of ,
Diffstat (limited to 'src')
-rw-r--r--src/descriptor_set_and_binding_parser.cc28
-rw-r--r--src/descriptor_set_and_binding_parser_test.cc12
-rw-r--r--src/tokenizer.cc3
3 files changed, 31 insertions, 12 deletions
diff --git a/src/descriptor_set_and_binding_parser.cc b/src/descriptor_set_and_binding_parser.cc
index 0d2a1ed..d563573 100644
--- a/src/descriptor_set_and_binding_parser.cc
+++ b/src/descriptor_set_and_binding_parser.cc
@@ -15,6 +15,8 @@
#include "src/descriptor_set_and_binding_parser.h"
#include <iostream>
+#include <limits>
+
#include "src/tokenizer.h"
namespace amber {
@@ -47,19 +49,35 @@ Result DescriptorSetAndBindingParser::Parse(const std::string& buffer_id) {
descriptor_set_ = 0;
}
- std::cout << token->AsString() << std::endl;
- if (!token->IsString() || token->AsString() != ",") {
+ if (!token->IsString())
+ return Result("Invalid buffer id: " + buffer_id);
+
+ auto& str = token->AsString();
+ if (str.size() < 2 || str[0] != ':')
return Result("Invalid buffer id: " + buffer_id);
+
+ auto substr = str.substr(1, str.size());
+ // Validate all characters are integers.
+ for (size_t i = 0; i < substr.size(); ++i) {
+ if (substr[i] < '0' || substr[i] > '9') {
+ return Result(
+ "Binding for a buffer must be non-negative integer, "
+ "but you gave: " +
+ substr);
+ }
}
- token = t.NextToken();
- if (!token->IsInteger() || token->AsInt32() < 0) {
+ uint64_t binding_val = strtoul(substr.c_str(), nullptr, 10);
+ if (binding_val > std::numeric_limits<uint32_t>::max())
+ return Result("binding value too large in probe ssbo command: " +
+ token->ToOriginalString());
+ if (static_cast<int32_t>(binding_val) < 0) {
return Result(
"Binding for a buffer must be non-negative integer, but you gave: " +
token->ToOriginalString());
}
- binding_ = token->AsUint32();
+ binding_ = static_cast<uint32_t>(binding_val);
return {};
}
diff --git a/src/descriptor_set_and_binding_parser_test.cc b/src/descriptor_set_and_binding_parser_test.cc
index 139a7ef..6010130 100644
--- a/src/descriptor_set_and_binding_parser_test.cc
+++ b/src/descriptor_set_and_binding_parser_test.cc
@@ -22,7 +22,7 @@ using DescriptorSetAndBindingParserTest = testing::Test;
TEST_F(DescriptorSetAndBindingParserTest, CommaAndBinding) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse(",1234");
+ Result r = parser.Parse(":1234");
ASSERT_TRUE(r.IsSuccess()) << r.Error();
EXPECT_EQ(0, parser.GetDescriptorSet());
@@ -40,7 +40,7 @@ TEST_F(DescriptorSetAndBindingParserTest, Binding) {
TEST_F(DescriptorSetAndBindingParserTest, DescSetAndBinding) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse("1234,5678");
+ Result r = parser.Parse("1234:5678");
ASSERT_TRUE(r.IsSuccess()) << r.Error();
EXPECT_EQ(1234, parser.GetDescriptorSet());
@@ -67,7 +67,7 @@ TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacterBetweenTwoNumbers) {
TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacterAfterComma) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse("1234,a5678");
+ Result r = parser.Parse("1234:a5678");
EXPECT_EQ(
"Binding for a buffer must be non-negative integer, but you gave: a5678",
r.Error());
@@ -75,7 +75,7 @@ TEST_F(DescriptorSetAndBindingParserTest, InvalidCharacterAfterComma) {
TEST_F(DescriptorSetAndBindingParserTest, NegativeDescSet) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse("-1234,5678");
+ Result r = parser.Parse("-1234:5678");
EXPECT_EQ(
"Descriptor set and binding for a buffer must be non-negative integer, "
"but you gave: -1234",
@@ -84,7 +84,7 @@ TEST_F(DescriptorSetAndBindingParserTest, NegativeDescSet) {
TEST_F(DescriptorSetAndBindingParserTest, NegativeBindingAfterComma) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse(",-1234");
+ Result r = parser.Parse(":-1234");
EXPECT_EQ(
"Binding for a buffer must be non-negative integer, but you gave: -1234",
r.Error());
@@ -101,7 +101,7 @@ TEST_F(DescriptorSetAndBindingParserTest, NegativeBinding) {
TEST_F(DescriptorSetAndBindingParserTest, DescSetAndNegativeBinding) {
DescriptorSetAndBindingParser parser;
- Result r = parser.Parse("1234,-5678");
+ Result r = parser.Parse("1234:-5678");
EXPECT_EQ(
"Binding for a buffer must be non-negative integer, but you gave: -5678",
r.Error());
diff --git a/src/tokenizer.cc b/src/tokenizer.cc
index 9eb6b1c..f24655c 100644
--- a/src/tokenizer.cc
+++ b/src/tokenizer.cc
@@ -157,7 +157,8 @@ std::unique_ptr<Token> Tokenizer::NextToken() {
if (tok_str.size() > 1 && tok_str[0] == '-')
tok->SetNegative();
- tok->SetOriginalString(tok_str);
+ tok->SetOriginalString(
+ tok_str.substr(0, static_cast<size_t>(final_pos - tok_str.c_str())));
// If the number isn't the whole token then move back so we can then parse
// the string portion.