Avoid out-of-bounds string indexing

In GetSubBuildDirAsOutputFile(), when the source directory is the same
as the build directory, an out-of-bounds string index is used and thus
triggers an assertion in libstdc++. Fix this by checking if the source
directory is longer than the build directory before using the latter's
length as the index.

Bug: 293
Fixes: fd9f2036f26d ("Fix output directories for generated source
                      files.")
Change-Id: I0b0756a01f181fb14f4bd97b45e1f441be3c0e8b
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/14080
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
diff --git a/src/gn/filesystem_utils.cc b/src/gn/filesystem_utils.cc
index 7a7078e..0f984a7 100644
--- a/src/gn/filesystem_utils.cc
+++ b/src/gn/filesystem_utils.cc
@@ -1036,8 +1036,10 @@
       // or `toolchain2/obj/BUILD_DIR/toolchain1/gen` which look surprising,
       // but guarantee unicity.
       result.value().append("BUILD_DIR/");
-      result.value().append(&source_dir_path[build_dir.size()],
-                            source_dir_path.size() - build_dir.size());
+      if (source_dir_path.size() > build_dir.size()) {
+        result.value().append(&source_dir_path[build_dir.size()],
+                              source_dir_path.size() - build_dir.size());
+      }
     } else {
       // The source dir is source-absolute, so we trim off the two leading
       // slashes to append to the toolchain object directory.