mlyszczek commented on code in PR #1460: URL: https://github.com/apache/nuttx-apps/pull/1460#discussion_r1054759231
########## logging/nxscope/nxscope.c: ########## @@ -0,0 +1,1138 @@ +/**************************************************************************** + * apps/logging/nxscope/nxscope.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <logging/nxscope/nxscope.h> + +#include "nxscope_internals.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxscope_frame_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_frame_send(FAR struct nxscope_s *s, uint8_t id, + FAR uint8_t *data, size_t dlen) +{ + size_t tx_i = 0; + int ret = OK; + + DEBUGASSERT(s); + DEBUGASSERT(data); + + /* Offset for hdr */ + + tx_i = s->proto_cmd->hdrlen; + + /* Copy data */ + + memcpy(&s->txbuf[tx_i], data, dlen); + tx_i += dlen; + + /* Finalize a new frame */ + + ret = PROTO_FRAME_FINAL(s, s->proto_cmd, id, s->txbuf, &tx_i); + if (ret < 0) + { + _err("ERROR: PROTO_FRAME_FINAL failed %d\n", ret); + goto errout; + } + + /* Send frame */ + + ret = INTF_SEND(s, s->intf_cmd, s->txbuf, tx_i); + if (ret < 0) + { + _err("ERROR: INTF_SEND failed %d\n", ret); + } + +errout: + return ret; +} + +/**************************************************************************** + * Name: nxscope_cmninfo_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_cmninfo_send(FAR struct nxscope_s *s) +{ + DEBUGASSERT(s); + + return nxscope_frame_send(s, + NXSCOPE_HDRID_CMNINFO, + (FAR uint8_t *)&s->cmninfo, + sizeof(struct nxscope_info_cmn_s)); +} + +/**************************************************************************** + * Name: nxscope_chinfo_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_chinfo_send(FAR struct nxscope_s *s, uint8_t ch) +{ + FAR uint8_t *data = NULL; + size_t namelen = 0; + size_t txlen = 0; + size_t tmp = 0; + int ret = OK; + + DEBUGASSERT(s); + + /* Allocate data for channel info */ + + namelen = strnlen(s->chinfo[ch].name, CHAN_NAMELEN_MAX) + 1; + tmp = sizeof(struct nxscope_chinfo_s) - sizeof(char *); + txlen = tmp + namelen; + + data = zalloc(txlen); Review Comment: Is that dynamic allocation really needed here? I don't know how often this is called, but it seems it will be called multiple times during system livetime. This will trash system memory on MCUs without MMU (memory fragmentation). It would be better to allocate `CHAN_NAMELEN_MAX + 1 + tmp` on stack since limits are well known during compile time. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org