Skip to content

Commit a36c623

Browse files
committed
Address #3395 (tool table corruption with zero-length comments)
Also be more careful with checking buffer space.
1 parent 775a6f3 commit a36c623

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/emc/tooldata/tooldata_common.cc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,27 @@ void tooldata_format_toolline (int idx,
236236
)
237237
{
238238
char tmp[CANON_TOOL_ENTRY_LEN-1] = {0};
239-
snprintf(tmp,sizeof(tmp),"T%-3d P%-3d"
239+
int space = sizeof(tmp);
240+
int len;
241+
len = snprintf(tmp,sizeof(tmp),"T%-3d P%-3d"
240242
,tdata.toolno
241243
,is_random_toolchanger ? idx : tdata.pocketno);
242-
strncat(formatted_line,tmp,CANON_TOOL_ENTRY_LEN-1);
244+
strncat(formatted_line,tmp,space);
245+
space -= len;
243246
// format zero float values as %.0f for brevity
244247
#define F_ITEM(item,letter) if (!ignore_zero_values || tdata.item) { \
245248
if (tdata.item) { \
246-
snprintf(tmp,sizeof(tmp)," " letter "%+f", tdata.item); \
249+
len = snprintf(tmp,sizeof(tmp)," " letter "%+f", tdata.item); \
247250
} else { \
248-
snprintf(tmp,sizeof(tmp)," " letter "%.0f",tdata.item); \
251+
len = snprintf(tmp,sizeof(tmp)," " letter "%.0f",tdata.item); \
249252
} \
250-
strncat(formatted_line,tmp,CANON_TOOL_ENTRY_LEN-1); \
253+
strncat(formatted_line,tmp,space); \
254+
space -= len; \
251255
}
252256
#define I_ITEM(item,letter) if (!ignore_zero_values || tdata.item) { \
253-
snprintf(tmp,sizeof(tmp)," " letter "%d",tdata.item); \
254-
strncat(formatted_line,tmp,CANON_TOOL_ENTRY_LEN-1); \
257+
len = snprintf(tmp,sizeof(tmp)," " letter "%d",tdata.item); \
258+
strncat(formatted_line,tmp,space); \
259+
space -= len; \
255260
}
256261

257262
F_ITEM(diameter, "D");
@@ -269,10 +274,13 @@ void tooldata_format_toolline (int idx,
269274
I_ITEM(orientation, "Q");
270275
#undef F_ITEM
271276
#undef I_ITEM
277+
// Stop tracking len and space here for current tool table format
272278
if (tdata.comment[0]) {
273-
snprintf(tmp,sizeof(tmp)," ;%s\n",tdata.comment); \
274-
strncat(formatted_line,tmp,CANON_TOOL_ENTRY_LEN-1); \
275-
}
279+
snprintf(tmp,space," ;%s\n",tdata.comment);
280+
} else {
281+
snprintf(tmp,space,"\n");
282+
}
283+
strncat(formatted_line,tmp,space);
276284
return;
277285
} // tooldata_format_toolline()
278286

0 commit comments

Comments
 (0)