From owner-dev-commits-src-main@freebsd.org Tue Jan 19 19:59:02 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 77C004DD69D; Tue, 19 Jan 2021 19:59:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DKzxf2x4Fz4cdf; Tue, 19 Jan 2021 19:59:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5774349D5; Tue, 19 Jan 2021 19:59:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10JJx2og048617; Tue, 19 Jan 2021 19:59:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10JJx2vP048616; Tue, 19 Jan 2021 19:59:02 GMT (envelope-from git) Date: Tue, 19 Jan 2021 19:59:02 GMT Message-Id: <202101191959.10JJx2vP048616@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Toomas Soome Subject: git: 52e3a7300d81 - main - loader: handle malloc failures in vbe_init, use consistent naming MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: tsoome X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 52e3a7300d817002d3b7f8336ba1716c9c2621a3 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Jan 2021 19:59:02 -0000 The branch main has been updated by tsoome: URL: https://cgit.FreeBSD.org/src/commit/?id=52e3a7300d817002d3b7f8336ba1716c9c2621a3 commit 52e3a7300d817002d3b7f8336ba1716c9c2621a3 Author: Toomas Soome AuthorDate: 2021-01-18 08:52:08 +0000 Commit: Toomas Soome CommitDate: 2021-01-19 19:58:46 +0000 loader: handle malloc failures in vbe_init, use consistent naming Handle malloc failures in vbe_init(). If it should so happen and we do get malloc failure in vbe_init(), use original mode list. Replace nitems with nentries to have naming consistency and avoid confusion with nitems() macro. Reported by: yuripv, rpokala --- stand/i386/libi386/vbe.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/stand/i386/libi386/vbe.c b/stand/i386/libi386/vbe.c index 8b2c74ade92e..0e9f6929ac05 100644 --- a/stand/i386/libi386/vbe.c +++ b/stand/i386/libi386/vbe.c @@ -565,8 +565,16 @@ vbe_init(void) gfx_state.tg_ctype = CT_INDEXED; gfx_state.tg_mode = 3; - if (vbe == NULL) + env_setenv("screen.textmode", EV_VOLATILE, "1", mode_set, + env_nounset); + env_setenv("vbe_max_resolution", EV_VOLATILE, NULL, mode_set, + env_nounset); + + if (vbe == NULL) { vbe = malloc(sizeof(*vbe)); + if (vbe == NULL) + return; + } if (vbe_mode == NULL) { vbe_mode = malloc(sizeof(*vbe_mode)); @@ -581,6 +589,7 @@ vbe_init(void) vbe = NULL; free(vbe_mode); vbe_mode = NULL; + return; } /* @@ -592,22 +601,21 @@ vbe_init(void) ; vbe_mode_list_size = (uintptr_t)p - (uintptr_t)ml; + + /* + * Since vbe_init() is used only once at very start of the loader, + * we assume malloc will not fail there, but in case it does, + * we point vbe_mode_list to memory pointed by VideoModePtr. + */ vbe_mode_list = malloc(vbe_mode_list_size); - if (vbe_mode_list == NULL) { - free(vbe); - vbe = NULL; - free(vbe_mode); - vbe_mode = NULL; - } - bcopy(ml, vbe_mode_list, vbe_mode_list_size); + if (vbe_mode_list == NULL) + vbe_mode_list = ml; + else + bcopy(ml, vbe_mode_list, vbe_mode_list_size); - /* reset VideoModePtr, so we will not have chance to use bad data. */ + /* reset VideoModePtr, to make sure, we only do use vbe_mode_list. */ vbe->VideoModePtr = 0; - env_setenv("screen.textmode", EV_VOLATILE, "1", mode_set, - env_nounset); - env_setenv("vbe_max_resolution", EV_VOLATILE, NULL, mode_set, - env_nounset); /* vbe_set_mode() will set up the rest. */ } @@ -758,7 +766,7 @@ vbe_find_mode_xydm(int x, int y, int depth, int m) struct modeinfoblock mi; uint16_t *farptr; uint16_t mode; - int idx, nitems, i; + int idx, nentries, i; memset(vbe, 0, sizeof (*vbe)); if (biosvbe_info(vbe) != VBE_SUCCESS) @@ -771,9 +779,9 @@ vbe_find_mode_xydm(int x, int y, int depth, int m) else i = depth; - nitems = vbe_mode_list_size / sizeof(*vbe_mode_list); + nentries = vbe_mode_list_size / sizeof(*vbe_mode_list); while (i > 0) { - for (idx = 0; idx < nitems; idx++) { + for (idx = 0; idx < nentries; idx++) { mode = vbe_mode_list[idx]; if (mode == 0xffff) break;