Indefinite integrals and changing variables Why is it legal to change the variable of an indefinite integral?

Consider
$$\int \dfrac{dx}{\cos x}$$
If one were to say, $\text{Let } u=\cos x$, do we not now technically have
$$\int_{u=\cos-\infty}^{u=\cos\infty}u^{-1}\dfrac{d\arccos u}{du} \cdot du$$
Which is clearly madness even before we consider that whatever $\cos \infty$ is, $\cos -\infty$ is too.

So my question is simply, what have I missed; why is it allowed?
 A: You don't even hint at the nature of the mysterious process that led you to the conclusion that the bounds of integration would be $\pm\infty$.  Let's try it with some actual bounds:
$$
\int_0^{\pi/4}\frac{dx}{\cos x} = \int_1^{\sqrt{2}/2} \frac{d\arccos u}{u} = \int_1^{\sqrt{2}/2} \frac{-du}{u\sqrt{1-u^2}}
$$
No "madness" is involved.
If you want to think about $\displaystyle\int_{-\infty}^\infty \frac{dx}{\cos x}$, you'll meet some unpleasant complications.
$$
\int_{-\infty}^\infty \frac{dx}{\cos x} = \sum_{n=-\infty}^\infty \int_{2\pi n}^{2\pi(n+1)} \frac{dx}{\cos x} = \sum_{n=-\infty}^\infty \int_0^{2\pi} \frac{dx}{\cos x}.
$$
That last sum would be $0$ if the value of the integral is $0$ and either $\pm\infty$ if it is any other number.  So let's think about
$$
\int_0^{2\pi} \frac{dx}{\cos x} = \left(\int_{-\pi/2}^{\pi/2} + \int_{\pi/2}^{3\pi/2} \right) \frac{dx}{\cos x} = \infty + (-\infty).
$$
Perhaps one could say that a sort of Cauchy principal value of this integral is $0$.  At each of the two locations $a$ of vertical asyptotes, one would have
$$
\lim_{\varepsilon\downarrow0}\left(\int_\cdots ^{a-\varepsilon} + \int_{a+\varepsilon}^ \cdots\right).
$$
Then one would get $0$.
But one should avoid substitutions that are not one-to-one.
As to why substitutions are valid, the short answer, which you'll find in every textbook is: the chain rule.
