| 129 | } |
| 130 | |
| 131 | void PathUtils::concatPath(PathName& result, |
| 132 | const PathName& first, |
| 133 | const PathName& second) |
| 134 | { |
| 135 | if (first.length() == 0) |
| 136 | { |
| 137 | result = second; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | result = first; |
| 142 | |
| 143 | // First path used to be from trusted sources like getRootDirectory, etc. |
| 144 | // Second path is mostly user-entered and must be carefully parsed to avoid hacking |
| 145 | if (second.length() == 0) |
| 146 | { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | ensureSeparator(result); |
| 151 | |
| 152 | PathName::size_type cur_pos = 0; |
| 153 | |
| 154 | for (PathName::size_type pos = 0; cur_pos < second.length(); cur_pos = pos + 1) |
| 155 | { |
| 156 | static const char separators[] = "/\\"; |
| 157 | static const PathName::size_type separatorsLen = |
| 158 | static_cast<PathName::size_type>(strlen(separators)); |
| 159 | |
| 160 | pos = second.find_first_of(separators, cur_pos, separatorsLen); |
| 161 | if (pos == PathName::npos) // simple name, simple handling |
| 162 | pos = second.length(); |
| 163 | |
| 164 | if (pos == cur_pos) // Empty piece, ignore |
| 165 | continue; |
| 166 | |
| 167 | if (pos == cur_pos + curr_dir_link_len && |
| 168 | memcmp(second.c_str() + cur_pos, curr_dir_link, curr_dir_link_len) == 0) // Current dir, ignore |
| 169 | { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | if (pos == cur_pos + up_dir_link_len && |
| 174 | memcmp(second.c_str() + cur_pos, up_dir_link, up_dir_link_len) == 0) // One dir up |
| 175 | { |
| 176 | if (result.length() < 2) |
| 177 | { |
| 178 | // We have nothing to cut off, ignore this piece (may be throw an error?..) |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | const PathName::size_type up_dir = result.find_last_of( |
| 183 | separators, result.length() - 2, separatorsLen); |
| 184 | |
| 185 | if (up_dir == PathName::npos) |
| 186 | continue; |
| 187 | |
| 188 | result.erase(up_dir + 1); |
nothing calls this directly
no test coverage detected