Skip to content

Commit

Permalink
reorder checks to avoid OOB in yacli_compact_spaces; guard memmove ag…
Browse files Browse the repository at this point in the history
…ainst 0/negative size
  • Loading branch information
bbonev committed Nov 12, 2024
1 parent 242c803 commit 6a7a743
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions yacli.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $Id: yacli.c,v 4.7 2024/11/12 21:26:21 bbonev Exp $
// $Id: yacli.c,v 4.8 2024/11/12 21:27:34 bbonev Exp $
//
// Copyright © 2015-2020 Boian Bonev ([email protected]) {{{
//
Expand Down Expand Up @@ -213,7 +213,7 @@ inline void yacli_set_showtermsize(yacli *cli,int v) { // {{{
cli->showtsize=!!v;
} // }}}

static char myver[]="\0Yet another command line interface library (https://github.com/bbonev/yacli) $Revision: 4.7 $\n\n"; // {{{
static char myver[]="\0Yet another command line interface library (https://github.com/bbonev/yacli) $Revision: 4.8 $\n\n"; // {{{
// }}}

inline const char *yacli_ver(void) { // {{{
Expand Down Expand Up @@ -1889,10 +1889,11 @@ static inline void yacli_compact_spaces(yacli *cli) { // {{{
poss=0;

for (;;) {
while (cli->buffer[posd]==' '&&cli->buffer[poss]==' '&&poss<cli->buflen)
while (posd<cli->buflen&&cli->buffer[posd]==' '&&poss<cli->buflen&&cli->buffer[poss]==' ')
poss++;
if (poss!=posd) {
memmove(cli->buffer+posd,cli->buffer+poss,cli->buflen-poss);
if (cli->buflen>poss)
memmove(cli->buffer+posd,cli->buffer+poss,cli->buflen-poss);
cli->buflen-=poss-posd;
if (cli->cursor>=poss)
cli->cursor-=poss-posd;
Expand Down

0 comments on commit 6a7a743

Please sign in to comment.