BUG: sefault in concat of CategoricalIndex
State: closed

 - [x] closes #16111
 - [ ] tests added / passed
 - [ ] passes ``git diff upstream/master --name-only -- '*.py' | flake8 --diff``
 - [ ] whatsnew entry


Labels:
name: Bug
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: Categorical
description: Categorical Data Type

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=h1) Report
> Merging [#16133](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/8d122e633a6b08e3f9dfad80050da42d789210d4?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16133/graphs/tree.svg?height=150&width=650&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16133      +/-   ##
==========================================
- Coverage   90.86%   90.86%   -0.01%     
==========================================
  Files         159      159              
  Lines       50771    50773       +2     
==========================================
+ Hits        46133    46134       +1     
- Misses       4638     4639       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.64% <100%> (-0.01%)` | :arrow_down: |
| #single | `40.33% <25%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `98.44% <100%> (ø)` | :arrow_up: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `90.68% <0%> (-0.35%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=footer). Last update [8d122e6...5b6be78](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=h1) Report
> Merging [#16133](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/8d122e633a6b08e3f9dfad80050da42d789210d4?src=pr&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16133/graphs/tree.svg?width=650&height=150&src=pr&token=eZ4WkYLtcO)](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16133      +/-   ##
==========================================
- Coverage   90.86%   90.83%   -0.03%     
==========================================
  Files         159      159              
  Lines       50771    50773       +2     
==========================================
- Hits        46133    46122      -11     
- Misses       4638     4651      +13
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.61% <100%> (-0.03%)` | :arrow_down: |
| #single | `40.33% <25%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `98.44% <100%> (ø)` | :arrow_up: |
| [pandas/plotting/\_converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb252ZXJ0ZXIucHk=) | `63.54% <0%> (-1.82%)` | :arrow_down: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `90.68% <0%> (-0.35%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=footer). Last update [8d122e6...5b6be78](https://codecov.io/gh/pandas-dev/pandas/pull/16133?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@chris-b1 your test example doesn't segfault for me on master, but the fix does work. Do the share the same underlying issue?
Yeah, the underlying problem is the we end up with a bad indexer, which is passed to `take_nd`, which doesn't bounds check, and end up with either a bad result (like the test) or a segfault.  Here is one that segfaults on master.

```
categories = list('vabcdefghijklmnopqrstu')

a = pd.Series(1, index=pd.CategoricalIndex(categories[:5], categories=categories))
b = pd.Series(2, index=pd.CategoricalIndex(categories[3:10], categories=categories))
c = pd.Series(3, index=pd.CategoricalIndex(categories[8:15], categories=categories))

result = pd.concat([a, b, c], axis=1)
```