From 1be6aa10c213242ec9759022eab36bc494dc4ac4 Mon Sep 17 00:00:00 2001 From: George Kulakowski <kulakowski@google.com> Date: Wed, 2 Jan 2019 13:13:44 -0800 Subject: [PATCH] [devmgr] Replace some gotos with lambdas Test: no functional change; booted under arm qemu with asan Change-Id: I5372d6ac791b678bf8536fb9120e803b73195e40 --- system/core/devmgr/devhost/devhost.cpp | 11 +++++++---- system/core/devmgr/devhost/rpc-server.cpp | 24 +++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/system/core/devmgr/devhost/devhost.cpp b/system/core/devmgr/devhost/devhost.cpp index a507c835d07..818405c1ec9 100644 --- a/system/core/devmgr/devhost/devhost.cpp +++ b/system/core/devmgr/devhost/devhost.cpp @@ -799,13 +799,16 @@ static ssize_t devhost_log_write_internal(uint32_t flags, const void* void_data, const char* data = static_cast<const char*>(void_data); size_t r = len; + auto flush_context = [&]() { + ctx->handle->write(flags, ctx->data, ctx->next); + ctx->next = 0; + }; + while (len-- > 0) { char c = *data++; if (c == '\n') { if (ctx->next) { -flush_ctx: - ctx->handle->write(flags, ctx->data, ctx->next); - ctx->next = 0; + flush_context(); } continue; } @@ -814,7 +817,7 @@ flush_ctx: } ctx->data[ctx->next++] = c; if (ctx->next == LOGBUF_MAX) { - goto flush_ctx; + flush_context(); } } return r; diff --git a/system/core/devmgr/devhost/rpc-server.cpp b/system/core/devmgr/devhost/rpc-server.cpp index 766fb65d9b1..cb2c562aa06 100644 --- a/system/core/devmgr/devhost/rpc-server.cpp +++ b/system/core/devmgr/devhost/rpc-server.cpp @@ -494,12 +494,15 @@ static zx_status_t fidl_file_writeat(void* ctx, const uint8_t* data, size_t coun static zx_status_t fidl_file_seek(void* ctx, int64_t offset, fuchsia_io_SeekOrigin start, fidl_txn_t* txn) { auto conn = static_cast<DevfsConnection*>(ctx); - size_t end, n; - end = conn->dev->GetSizeOp(); + auto bad_args = [&]() { + return fuchsia_io_FileSeek_reply(txn, ZX_ERR_INVALID_ARGS, 0); + }; + size_t end = conn->dev->GetSizeOp(); + size_t n; switch (start) { case fuchsia_io_SeekOrigin_START: if ((offset < 0) || ((size_t)offset > end)) { - goto bad_args; + return bad_args(); } n = offset; break; @@ -511,13 +514,13 @@ static zx_status_t fidl_file_seek(void* ctx, int64_t offset, fuchsia_io_SeekOrig // if negative seek if (n > conn->io_off) { // wrapped around - goto bad_args; + return bad_args(); } } else { // positive seek if (n < conn->io_off) { // wrapped around - goto bad_args; + return bad_args(); } } break; @@ -527,27 +530,24 @@ static zx_status_t fidl_file_seek(void* ctx, int64_t offset, fuchsia_io_SeekOrig // if negative or exact-end seek if (n > end) { // wrapped around - goto bad_args; + return bad_args(); } } else { if (n < end) { // wrapped around - goto bad_args; + return bad_args(); } } break; default: - goto bad_args; + return bad_args(); } if (n > end) { // devices may not seek past the end - goto bad_args; + return bad_args(); } conn->io_off = n; return fuchsia_io_FileSeek_reply(txn, ZX_OK, conn->io_off); - -bad_args: - return fuchsia_io_FileSeek_reply(txn, ZX_ERR_INVALID_ARGS, 0); } static zx_status_t fidl_file_truncate(void* ctx, uint64_t length, fidl_txn_t* txn) { -- GitLab