From nobody Wed Nov 27 04:56:27 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1700516755; cv=none; d=zohomail.com; s=zohoarc; b=YOmkjUmprVhgGWTviDJ/ri7QHGuo2S90R9xOdBToaMaCD5b3x98kKTT8eoa+tafwdhu/BVIXjFOHO3444sBVpvJ/Fyuz2srzLEW2GDp3ChxJhsiyfsmy+6wEqvD4gMomqyEnLS/v1YKbV26g0V/bHv7kBpRraN7K8C2DvB3CDCc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1700516755; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yOi8zo4jI/uumYmIqWG8dRZhTC7Umkqv7tQVWJif/z0=; b=GpmoU7/lK2ylQMeZptGmeTV3NzzMHb1iLZ10FFZ5Q4zHab+ejCoHJzvBdgGiKpr2OoLw0VYxxYP8z2e2aefQY8kKke0JUOraaa/AzOlkvbc0nEmWWMLsXTMwDMO7Hl3bGIFnBQxMBB3xZsmg6bn28x3iOnCVU2j7Z5CvJ9N3C0w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1700516755107337.97838815344244; Mon, 20 Nov 2023 13:45:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5C5O-0005bz-Dt; Mon, 20 Nov 2023 16:45:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5C5E-0005U7-KF for qemu-devel@nongnu.org; Mon, 20 Nov 2023 16:45:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5C59-0005wU-Pd for qemu-devel@nongnu.org; Mon, 20 Nov 2023 16:45:30 -0500 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-657-2X9WDuReMtOfcfuBPuJepw-1; Mon, 20 Nov 2023 16:45:25 -0500 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-9fcfa742e49so116719066b.2 for ; Mon, 20 Nov 2023 13:45:25 -0800 (PST) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id h11-20020a170906530b00b009d23e00a90esm4365376ejo.24.2023.11.20.13.45.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 13:45:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700516726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yOi8zo4jI/uumYmIqWG8dRZhTC7Umkqv7tQVWJif/z0=; b=c/mWOfwS11M63g+Pt2NR/Vi4dMTfr1BjjrymyDZ+GVvMR3flosOooWTcV1Gti8DTFx4624 5s+/A5O1+cnJ3KgAu2PMD7m/s+f4c0FK2AovkP9HLNebeqD5tfl33TzlfonKKYhimZgZES XPXMuAVlSanF3Xjr4CObQKZ5j8XDW9E= X-MC-Unique: 2X9WDuReMtOfcfuBPuJepw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700516723; x=1701121523; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yOi8zo4jI/uumYmIqWG8dRZhTC7Umkqv7tQVWJif/z0=; b=NYwmA5qTavSoQsbbsX2SjjKD0CeROU05o/cIM+os4Zhkp1fWc9tpIdFwAV60xPKzLi T4NamtPoxwCO6Xe+oF9H+4fssUGOpmTQkkpHjR8M8yp3fTboVPDBSPq9bzB1Yyth7yTy 9AhLgF4X4foNZg/Gh1xBqkRXU1xCjMr/RRYMWCbRbzBysbchifKEaaB9OKdzHhboKwDB PHqdcNKJ4fFFHhwFZRjLNvDBR4B8Ka0ifvlornirYAK+Cd25KSrEpyGGOM09/QWjxDiq 8ynQAAybZo7+5E0DQkeswEby5DvytXkv+p/hWy3p1tQCcgHEoIPOk777xUU1HY7rCIna uL3w== X-Gm-Message-State: AOJu0YzBWIzAzDYUz1Aki7EbbFti6Uzfm5YlxJrez0eFPlk9Z3aw90Hh vYTkWCKeqOaV2PX/4Wj+8LJo4+5UiS3kT6YcsCqk0xDQACIy1Th0yxGgzVLwOEGUDOIT/VSa+PY 6Ms3P1XEyNvjNTCi21TgHgL4o5skcz0lJ4i48RiGtlrjBQylJeTdCLMCr8Sjlr8FFQ/9UNUGdMT A= X-Received: by 2002:a17:906:a86:b0:9fd:a5da:cff7 with SMTP id y6-20020a1709060a8600b009fda5dacff7mr3577216ejf.29.1700516723393; Mon, 20 Nov 2023 13:45:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdlY0f+gM4n0j5GU3iqvKD4uvFJdi2YTP59pvNPLv8Fpasxw8gtjs/swSvSWiwOPow8+JXPQ== X-Received: by 2002:a17:906:a86:b0:9fd:a5da:cff7 with SMTP id y6-20020a1709060a8600b009fda5dacff7mr3577206ejf.29.1700516722908; Mon, 20 Nov 2023 13:45:22 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Akihiko Odaki Subject: [PATCH v2] disas/cris: Pass buffer size to format_dec() to avoid overflow warning Date: Mon, 20 Nov 2023 22:45:20 +0100 Message-ID: <20231120214520.59431-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.035, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1700516755437100001 From: Philippe Mathieu-Daud=C3=A9 Propagate the buffer size to format_dec() and use snprintf(). This should silence this UBSan -Wformat-overflow warning: In file included from /usr/include/stdio.h:906, from include/qemu/osdep.h:114, from ../disas/cris.c:21: In function 'sprintf', inlined from 'format_dec' at ../disas/cris.c:1737:3, inlined from 'print_with_operands' at ../disas/cris.c:2477:12, inlined from 'print_insn_cris_generic.constprop' at ../disas/cris.c:2= 690:8: /usr/include/bits/stdio2.h:30:10: warning: null destination pointer [-Wfo= rmat-overflow=3D] 30 | return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 31 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 32 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ Reported-by: Akihiko Odaki Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-ID: <20231120132222.82138-1-philmd@linaro.org> [Rewritten to fix logic and avoid repeated expression. - Paolo] Signed-off-by: Paolo Bonzini Reviewed-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- disas/cris.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/disas/cris.c b/disas/cris.c index 0b0a3fb9165..409a224c5d1 100644 --- a/disas/cris.c +++ b/disas/cris.c @@ -1731,10 +1731,10 @@ format_hex (unsigned long number, unsigned (=3D=3D 0). */ =20 static char * -format_dec (long number, char *outbuffer, int signedp) +format_dec (long number, char *outbuffer, size_t outsize, int signedp) { last_immediate =3D number; - sprintf (outbuffer, signedp ? "%ld" : "%lu", number); + snprintf (outbuffer, outsize, signedp ? "%ld" : "%lu", number); =20 return outbuffer + strlen (outbuffer); } @@ -1876,6 +1876,12 @@ print_flags (struct cris_disasm_data *disdata, unsig= ned int insn, char *cp) return cp; } =20 +#define FORMAT_DEC(number, tp, signedp) \ + format_dec (number, tp, ({ \ + assert(tp >=3D temp && tp <=3D temp + sizeof(temp)); \ + temp + sizeof(temp) - tp; \ + }), signedp) + /* Print out an insn with its operands, and update the info->insn_type fields. The prefix_opcodep and the rest hold a prefix insn that is supposed to be output as an address mode. */ @@ -2105,7 +2111,7 @@ print_with_operands (const struct cris_opcode *opcode= p, if ((*cs =3D=3D 'z' && (insn & 0x20)) || (opcodep->match =3D=3D BDAP_QUICK_OPCODE && (nbytes <=3D 2 || buffer[1 + nbytes] =3D=3D 0))) - tp =3D format_dec (number, tp, signedp); + tp =3D FORMAT_DEC (number, tp, signedp); else { unsigned int highbyte =3D (number >> 24) & 0xff; @@ -2241,7 +2247,7 @@ print_with_operands (const struct cris_opcode *opcode= p, with_reg_prefix); if (number >=3D 0) *tp++ =3D '+'; - tp =3D format_dec (number, tp, 1); + tp =3D FORMAT_DEC (number, tp, 1); =20 info->flags |=3D CRIS_DIS_FLAG_MEM_TARGET_IS_REG; info->target =3D (prefix_insn >> 12) & 15; @@ -2340,7 +2346,7 @@ print_with_operands (const struct cris_opcode *opcode= p, { if (number >=3D 0) *tp++ =3D '+'; - tp =3D format_dec (number, tp, 1); + tp =3D FORMAT_DEC (number, tp, 1); } } else @@ -2397,7 +2403,7 @@ print_with_operands (const struct cris_opcode *opcode= p, break; =20 case 'I': - tp =3D format_dec (insn & 63, tp, 0); + tp =3D FORMAT_DEC (insn & 63, tp, 0); break; =20 case 'b': @@ -2426,11 +2432,11 @@ print_with_operands (const struct cris_opcode *opco= dep, break; =20 case 'c': - tp =3D format_dec (insn & 31, tp, 0); + tp =3D FORMAT_DEC (insn & 31, tp, 0); break; =20 case 'C': - tp =3D format_dec (insn & 15, tp, 0); + tp =3D FORMAT_DEC (insn & 15, tp, 0); break; =20 case 'o': @@ -2463,7 +2469,7 @@ print_with_operands (const struct cris_opcode *opcode= p, if (number > 127) number =3D number - 256; =20 - tp =3D format_dec (number, tp, 1); + tp =3D FORMAT_DEC (number, tp, 1); *tp++ =3D ','; tp =3D format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix); } @@ -2474,7 +2480,7 @@ print_with_operands (const struct cris_opcode *opcode= p, break; =20 case 'i': - tp =3D format_dec ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp,= 1); + tp =3D FORMAT_DEC ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp,= 1); break; =20 case 'P': --=20 2.41.0