From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: Sergey Ostanevich <sergos@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: Re: [Tarantool-patches] [PATCH] core: fix static_alloc buffer overflow Date: Mon, 2 Nov 2020 22:09:29 +0100 [thread overview] Message-ID: <fd5efc02-a732-ab08-d502-257140674ed6@tarantool.org> (raw) In-Reply-To: <20201102131945.GB517@tarantool.org> > Thanks for the investigation! My bad, I used MIN as a function with > sematics of all agruments calculated before call. You're right - in case > of define it can cause a double call. > > The SNPRINT although leaves some questions to me: in case 'written' is > more or equal to 'size', it forces '_buf' to be set to NULL. But in the > sio_socketname_to_buffer() there's no check for NULL between the calls. > A call to snprintf() delivers a segfault, at least for Mac. Woops, SNPRINT is used a lot in the code. If it is true, we need to fix SNPRINT. > Also, factoring out 18 loc out of a function with a total of 24 seems > redundant - IMVHO. With your patch it is also an issue with alignment. See below. I tried to fix it in-place, but it was too ugly due to too big indentation. > --- a/src/lib/core/sio.c > +++ b/src/lib/core/sio.c > @@ -53,20 +53,22 @@ sio_socketname(int fd) > int save_errno = errno; > int name_size = 2 * SERVICE_NAME_MAXLEN; > char *name = static_alloc(name_size); > - int n = snprintf(name, name_size, "fd %d", fd); > + int to_be_printed = snprintf(name, name_size, "fd %d", fd); > + int n = MIN(to_be_printed, name_size); > if (fd >= 0) { > struct sockaddr_storage addr; > socklen_t addrlen = sizeof(addr); > int rc = getsockname(fd, (struct sockaddr *) &addr, &addrlen); > if (rc == 0) { > - n += snprintf(name + n, name_size - n, ", aka %s", > + to_be_printed = snprintf(name + n, name_size - n, ", aka %s", > sio_strfaddr((struct sockaddr *)&addr, > addrlen)); Here sio_strfaddr( should get +17 spaces, and that makes it hard to read. So I extracted everything to a new function with lower indentation.
next prev parent reply other threads:[~2020-11-02 21:09 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-10-23 15:13 Sergey Ostanevich 2020-10-23 20:06 ` Vladislav Shpilevoy 2020-10-29 22:56 ` Vladislav Shpilevoy 2020-10-30 7:07 ` Cyrill Gorcunov 2020-10-31 16:33 ` Vladislav Shpilevoy 2020-11-02 13:19 ` Sergey Ostanevich 2020-11-02 21:09 ` Vladislav Shpilevoy [this message] 2020-11-02 21:18 ` Cyrill Gorcunov 2020-11-02 21:43 ` Vladislav Shpilevoy 2020-11-02 21:47 ` Cyrill Gorcunov 2020-11-03 13:59 ` Sergey Ostanevich 2020-11-03 14:08 ` Cyrill Gorcunov 2020-11-03 22:59 ` Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=fd5efc02-a732-ab08-d502-257140674ed6@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=sergos@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH] core: fix static_alloc buffer overflow' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox