Edit: thank you for commenting. I've edited the question to use lstnewenvironment but it keeps giving the same error. I will keep the question here while I go read the lstlistings manual.
The following code works for me when used inside the document:
\begin{adjustwidth*}{0pt}{-5cm}
\begin{lstlisting}
function test() { return (void *) test_func(a, b, 3); }
\end{lstlisting}
\end{adjustwidth*}
But when I want to create a custom environment for it, Overleaf gives me the error
job aborted, no legal \end found.
MWE:
\documentclass{article}
\usepackage[right=10cm]{geometry} % Create a large right margin
\usepackage{listings}
\usepackage{changepage}
% Create a new environment for code samples that can extend into the outer margin.
\lstnewenvironment{config}{%
\begin{adjustwidth}{0pt}{-5cm}
}{%
\end{adjustwidth}
}
\begin{document}
\begin{config}
function test() { return (void *) test_func(a, b, 3); } asdfasdfa asdf
\end{config}
\end{document}

adjustwidthenvironment butlstlisting.\newenvironment{config}{\begin{lstlisting}}{\end{lstlisting}}causes the same error message that you observewith your original definition of theconfigenvironment. – leandriis Jul 21 '20 at 07:08lstlisting, thelistingspackage offers\lstnewenvironment. See also: lstlisting in a newenvironment – leandriis Jul 21 '20 at 07:09