Skip to content

Commit b8a11d7

Browse files
committed
halcmd: fix string diagnostics in backslash handling
1 parent 5d1c0bf commit b8a11d7

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/hal/utils/halcmd_main.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@
6060
#include <fnmatch.h>
6161
#include <search.h>
6262

63+
static char *vseprintf(char *buf, char *ebuf, const char *fmt, va_list ap) {
64+
int result = vsnprintf(buf, ebuf-buf, fmt, ap);
65+
if(result < 0) return ebuf;
66+
else if(buf + result > ebuf) return ebuf;
67+
else return buf + result;
68+
}
69+
70+
static char *seprintf(char *buf, char *ebuf, const char *fmt, ...) {
71+
va_list ap;
72+
va_start(ap, fmt);
73+
char *result = vseprintf(buf, ebuf, fmt, ap);
74+
va_end(ap);
75+
return result;
76+
}
77+
6378
static int get_input(FILE *srcfile, char *buf, size_t bufsize);
6479
static void print_help_general(int showR);
6580
static int release_HAL_mutex(void);
@@ -255,49 +270,36 @@ int main(int argc, char **argv)
255270
}
256271
} else {
257272
int extend_ct = 0; // extend lines with backslash (\)
258-
char *elinenext = 0;
273+
char eline [(LINELEN + 2) * (MAX_EXTEND_LINES + 1)] = {0,};
274+
char *elineptr=eline, *elineend=eline + sizeof(eline);
259275
/* read command line(s) from 'srcfile' */
260276
while (get_input(srcfile, raw_buf, MAX_CMD_LEN)) {
261277
char *tokens[MAX_TOK+1];
262-
char eline [(LINELEN + 2) * (MAX_EXTEND_LINES + 1)];
263-
char *elineptr;
264278
int newLinePos;
265279

266280
halcmd_set_linenumber(linenumber++);
267281

268282
newLinePos = (int)strlen(raw_buf) - 1; // interactive
269-
if (raw_buf[newLinePos] == '\n') { newLinePos--; } // tty
283+
if (raw_buf[newLinePos] == '\n') { raw_buf[newLinePos]=0; newLinePos--; } // tty
270284

271-
if (newLinePos > 0 && raw_buf[newLinePos] == '\\') { // backslash
285+
if (raw_buf[newLinePos] == '\\') { // backslash
272286
raw_buf[newLinePos] = 0;
273287
newLinePos++;
274288
if (!extend_ct) { //first extend
275289
if (prompt == prompt_interactive) prompt = prompt_continue;
276-
elineptr = eline;
277-
strncpy(elineptr,raw_buf,strlen(raw_buf));
278-
elinenext = elineptr + strlen(raw_buf);
279-
} else { // subsequent extends
280-
strncpy(elinenext,raw_buf,newLinePos);
281-
elinenext = elinenext + strlen(raw_buf);
282290
}
283-
*elinenext = 0;
291+
elineptr = seprintf(elineptr, elineend, "%s", raw_buf);
284292
extend_ct++;
285293
continue; // get next line to extend
286294
} else { // no backslash
287-
if (extend_ct) { // extend finished
288-
strncpy(elinenext,raw_buf,strlen(raw_buf));
289-
*(eline+strlen(eline)+0)='\n';
290-
elinenext = elinenext + strlen(raw_buf);
291-
*elinenext = 0;
292-
elineptr = eline;
293-
}
295+
elineptr = seprintf(elineptr, elineend, "%s", raw_buf);
294296
}
295-
if (!extend_ct) { elineptr = (char*)raw_buf; }
296297
extend_ct = 0;
297298
if (prompt == prompt_continue) { prompt = prompt_interactive; }
298299

299300
/* remove comments, do var substitution, and tokenise */
300-
retval = halcmd_preprocess_line(elineptr, tokens);
301+
retval = halcmd_preprocess_line(eline, tokens);
302+
301303
if(echo_mode) {
302304
halcmd_echo("%s\n", eline);
303305
}
@@ -324,6 +326,8 @@ int main(int argc, char **argv)
324326
/* exit from loop */
325327
break;
326328
}
329+
elineptr=eline;
330+
*eline = 0;
327331
} //while get_input()
328332
extend_ct=0;
329333
}

0 commit comments

Comments
 (0)