Fix invalid access 46/286946/1
authorChanggyu Choi <changyu.choi@samsung.com>
Tue, 17 Jan 2023 07:16:54 +0000 (16:16 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 17 Jan 2023 07:16:54 +0000 (16:16 +0900)
If std::string.length() is zero, then back() method's behavior is
undefined.

Change-Id: I5ccededf83b76058e91ebefec6b11ad8ff8f1db6
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
idlc/gen/c_gen_base.cc
idlc/gen/dart_gen_base.cc
idlc/gen_cion/c_cion_gen_base.cc

index 0480a6e..df2ac1a 100644 (file)
@@ -232,7 +232,10 @@ std::string CGeneratorBase::SmartIndent(std::string lines) {
       tab_size--;
     }
 
-    back = line.back();
+    back = "__INVALID__";
+    if (line.length() > 0)
+      back = line.back();
+
     if (back != ":" && line.length() > 0)
       tab += Tab(tab_size);
 
@@ -250,9 +253,7 @@ std::string CGeneratorBase::SmartIndent(std::string lines) {
     if (back == "," || back == "\\")
       continuous = true;
 
-    if (line.empty() ||
-        line.length() == 0 ||
-        std::all_of(line.begin(), line.end(), isspace)) {
+    if (line.empty() || std::all_of(line.begin(), line.end(), isspace)) {
       std::string n_line = Trim(next_line);
       if (n_line.empty() ||
           n_line.length() == 0 ||
@@ -296,7 +297,6 @@ std::string CGeneratorBase::SmartIndent(std::string lines) {
         if_statement = true;
       }
 
-      back = line.back();
       found = line.find("while (");
       if (found != std::string::npos && back != ";") {
         tab_size++;
index 008b5c7..4403bfc 100644 (file)
@@ -507,7 +507,10 @@ std::string DartGeneratorBase::SmartIndent(std::string lines) {
       tab += Tab(2);
 
     // Checks whether switch case is end or not.
-    line_back = line.back();
+    line_back = "__INVALID__";
+    if (line.length() > 0)
+      line_back = line.back();
+
     if (!prev_tab_size.empty() && prev_tab_size.top() == (tab_size - 1) &&
         line_back == "}") {
       prev_tab_size.pop();
index 85d9590..78e9317 100644 (file)
@@ -231,7 +231,10 @@ std::string CCionGeneratorBase::SmartIndent(std::string lines) {
       tab_size--;
     }
 
-    back = line.back();
+    back = "__INVALID__";
+    if (line.length() > 0)
+      back = line.back();
+
     if (back != ":" && line.length() > 0)
       tab += Tab(tab_size);
 
@@ -249,9 +252,7 @@ std::string CCionGeneratorBase::SmartIndent(std::string lines) {
     if (back == "," || back == "\\")
       continuous = true;
 
-    if (line.empty() ||
-        line.length() == 0 ||
-        std::all_of(line.begin(), line.end(), isspace)) {
+    if (line.empty() || std::all_of(line.begin(), line.end(), isspace)) {
       std::string n_line = Trim(next_line);
       if (n_line.empty() ||
           n_line.length() == 0 ||
@@ -295,7 +296,6 @@ std::string CCionGeneratorBase::SmartIndent(std::string lines) {
         if_statement = true;
       }
 
-      back = line.back();
       found = line.find("while (");
       if (found != std::string::npos && back != ";") {
         tab_size++;