From owner-svn-src-head@freebsd.org Thu May 17 17:45:48 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 11B2BEDB259; Thu, 17 May 2018 17:45:48 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B84DC6C741; Thu, 17 May 2018 17:45:47 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 957534ADB; Thu, 17 May 2018 17:45:47 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4HHjlIC064909; Thu, 17 May 2018 17:45:47 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4HHjlrr064908; Thu, 17 May 2018 17:45:47 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <201805171745.w4HHjlrr064908@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Thu, 17 May 2018 17:45:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r333742 - head/tools/tools/intel-ucode-split X-SVN-Group: head X-SVN-Commit-Author: emaste X-SVN-Commit-Paths: head/tools/tools/intel-ucode-split X-SVN-Commit-Revision: 333742 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 May 2018 17:45:48 -0000 Author: emaste Date: Thu May 17 17:45:47 2018 New Revision: 333742 URL: https://svnweb.freebsd.org/changeset/base/333742 Log: intel-ucode-split: incorporate review feedback, using asprintf As reported by delphij in review D15443 asprintf cleans this up a little by avoiding hardcoded buffer sizes. Reported by: delphij Modified: head/tools/tools/intel-ucode-split/intel-ucode-split.c Modified: head/tools/tools/intel-ucode-split/intel-ucode-split.c ============================================================================== --- head/tools/tools/intel-ucode-split/intel-ucode-split.c Thu May 17 17:40:06 2018 (r333741) +++ head/tools/tools/intel-ucode-split/intel-ucode-split.c Thu May 17 17:45:47 2018 (r333742) @@ -54,12 +54,13 @@ struct microcode_update_header { /* * SDM vol 2A CPUID EAX = 01h Returns Model, Family, Stepping Information. - * 9 chars including the NUL terminator will be written to buf. + * Caller must free the returned string. */ static char * -format_signature(char *buf, uint32_t signature) +format_signature(uint32_t signature) { + char *buf; unsigned family, model, stepping; family = (signature & 0xf00) >> 8; @@ -69,24 +70,26 @@ format_signature(char *buf, uint32_t signature) model += (signature & 0xf0000) >> 12; if (family == 0x0f) family += (signature & 0xff00000) >> 20; - sprintf(buf, "%02x-%02x-%02x", family, model, stepping); + asprintf(&buf, "%02x-%02x-%02x", family, model, stepping); + if (buf == NULL) + err(1, "asprintf"); return (buf); } static void dump_header(const struct microcode_update_header *hdr) { - char buf[16]; + char *sig_str; int i; bool platformid_printed; + sig_str = format_signature(hdr->processor_signature); printf("header version\t0x%x\n", hdr->header_version); printf("revision\t0x%x\n", hdr->update_revision); printf("date\t\t0x%x\t%04x-%02x-%02x\n", hdr->date, hdr->date & 0xffff, (hdr->date & 0xff000000) >> 24, (hdr->date & 0xff0000) >> 16); - printf("signature\t0x%x\t\t%s\n", hdr->processor_signature, - format_signature(buf, hdr->processor_signature)); + printf("signature\t0x%x\t\t%s\n", hdr->processor_signature, sig_str); printf("checksum\t0x%x\n", hdr->checksum); printf("loader revision\t0x%x\n", hdr->loader_revision); printf("processor flags\t0x%x", hdr->processor_flags); @@ -102,6 +105,7 @@ dump_header(const struct microcode_update_header *hdr) hdr->data_size != 0 ? hdr->data_size : 2000); printf("size\t\t0x%x\t\t0x%x\n", hdr->total_size, hdr->total_size != 0 ? hdr->total_size : 2048); + free(sig_str); } static void @@ -116,8 +120,7 @@ int main(int argc, char *argv[]) { struct microcode_update_header hdr; - char output_file[128]; - char *buf; + char *buf, *output_file, *sig_str; size_t len, resid; ssize_t rv; int c, ifd, ofd; @@ -163,9 +166,12 @@ main(int argc, char *argv[]) if (vflag) dump_header(&hdr); - format_signature(output_file, hdr.processor_signature); - sprintf(output_file + strlen(output_file), ".%02x", + sig_str = format_signature(hdr.processor_signature); + asprintf(&output_file, "%s.%02x", sig_str, hdr.processor_flags & 0xff); + free(sig_str); + if (output_file == NULL) + err(1, "asprintf"); ofd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (ofd < 0) err(1, "open"); @@ -194,5 +200,6 @@ main(int argc, char *argv[]) if (vflag) printf("written to %s\n\n", output_file); close(ofd); + free(output_file); } }