Keep aspect ratio when resizing?

I am also looking for an efficient way to keep the aspect ratio. Black borders or something similar are not an option. For now I do this after receiving SDL_EVENT_WINDOW_RESIZED (SDL3 pre-release):

void Screen_SizeChanged(void) {
	float scale;
	int h;

	SDL_GetWindowSize(sdlWindow, NULL, &h);
	scale = (float)h / height;
	SDL_SetWindowSize(sdlWindow, width*scale, height*scale);
	SDL_SetRenderScale(sdlRenderer, scale*dpiFactor, scale*dpiFactor);
}

This works on macOS (no flickering as described above) but seems to be a bit too complicated. Does anyone have a simpler way to keep aspect ratio?

Note: dpiFactor is a float that is set during initialisation and represents the amount of phyical pixels per logical pixel, width and height represent the logical size of the content.