This seems more like a coding problem than a C problem. Most people would at least validate input before allocating anything.

A HTTP request processing layer would typically have some configuration of the maximum content size that can be received in one request and reject anything larger.

That's why, e.g., you have to mess with a "php.ini" file to get your self-hosted webmail to handle large attachments (which are uploads from the browser UI to the back-end).