From patchwork Wed Nov 18 22:30:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Clayton X-Patchwork-Id: 7653131 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EDC12BF90C for ; Wed, 18 Nov 2015 22:33:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2C192204A2 for ; Wed, 18 Nov 2015 22:33:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4A2002049E for ; Wed, 18 Nov 2015 22:33:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933583AbbKRWcB (ORCPT ); Wed, 18 Nov 2015 17:32:01 -0500 Received: from mail-pa0-f43.google.com ([209.85.220.43]:35238 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933422AbbKRWb7 (ORCPT ); Wed, 18 Nov 2015 17:31:59 -0500 Received: by pacej9 with SMTP id ej9so58144204pac.2; Wed, 18 Nov 2015 14:31:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=9yzM6GPHoi7m9sidyX/IjKiwwh1IkYDbWSUi/Wb0M5c=; b=cov1fyc+vWe+dMzfI9UIAN9kKV2s+IJePdABgCh6W79+25gby6jEJF6Kfhqi/K3v0b 2j3q6+sWD5yNWB2DFyM1QmK1BG/V+fQeMffMK14uFoJNTmL2jisAl795obiAM0w576yP 73HCujhAk0JfDowqrgcKED77djd/hii4hjdiIL9uGzF2u2rKDA4IZyYn2/p7/j32azMX xLaqBOYVF62Ae2TRz5/eQQo0DGg80lIljCCD6IFjo8cJ5YENrHTjz+nrZguXPxNrb6rJ fLExaVZlD08Ye3umsdq7jjvApm6aBKCLJwplC63ZVCxSSXfeDoAJqLUPRsTV3hHhmuSg y4tw== X-Received: by 10.68.242.105 with SMTP id wp9mr5651159pbc.49.1447885919148; Wed, 18 Nov 2015 14:31:59 -0800 (PST) Received: from localhost.localdomain (68-185-59-186.static.knwc.wa.charter.com. [68.185.59.186]) by smtp.gmail.com with ESMTPSA id we9sm5836249pab.3.2015.11.18.14.31.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 Nov 2015 14:31:58 -0800 (PST) From: Joshua Clayton To: Mark Brown , linux-spi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Anton Bondarenko , Joshua Clayton Subject: [PATCH v2 2/6] spi: spidev_test: transfer_escaped_string function Date: Wed, 18 Nov 2015 14:30:38 -0800 Message-Id: X-Mailer: git-send-email 2.5.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move the input_tx code into its own small function. This cleans up some variables from main() that are used only here. While we are at it, check malloc calls instead of assuming they succeed. Signed-off-by: Joshua Clayton --- tools/spi/spidev_test.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 135b3f5..f9d2957 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -249,13 +249,30 @@ static void parse_opts(int argc, char *argv[]) } } +static void transfer_escaped_string(int fd, char *str) +{ + size_t size = strlen(str + 1); + uint8_t *tx; + uint8_t *rx; + + tx = malloc(size); + if (!tx) + pabort("can't allocate tx buffer"); + + rx = malloc(size); + if (!rx) + pabort("can't allocate rx buffer"); + + size = unescape((char *)tx, str, size); + transfer(fd, tx, rx, size); + free(rx); + free(tx); +} + int main(int argc, char *argv[]) { int ret = 0; int fd; - uint8_t *tx; - uint8_t *rx; - int size; parse_opts(argc, argv); @@ -300,17 +317,10 @@ int main(int argc, char *argv[]) printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); - if (input_tx) { - size = strlen(input_tx+1); - tx = malloc(size); - rx = malloc(size); - size = unescape((char *)tx, input_tx, size); - transfer(fd, tx, rx, size); - free(rx); - free(tx); - } else { + if (input_tx) + transfer_escaped_string(fd, input_tx); + else transfer(fd, default_tx, default_rx, sizeof(default_tx)); - } close(fd);