macExpandString writes out of boundary
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
EPICS Base |
Fix Released
|
Medium
|
Eric Norum |
Bug Description
Andrew and others,
I’ve just found a bug in libCom/macLib. I had a long command line (dbLoadRecords(…)) in a startup script, with several macros in it. When the string is expanded it is more than 256 characters, so macEnvExpand has to go through an extra iteration. When it calls free(dest), Windows (win32-x86 architecture) detects heap corruption. It says that values past the end of “dest” have been modified. This almost certainly means that macExpandString has written past the end of “dest”. I suspect it is by 1 character.
To track it down I set handle->debug=3 in macEnvExpand, and got the following output.
dbLoadRecords(
macExpandString( J:/epics/
trans-> entry = 0012FC60, level = 0, maxlen = 256, discard = F, rawval = J:/epics/
<-trans level = 0, length = 59, value = J:/epics/
macExpandString() -> 59
macDeleteHandle()
macExpandString( dbLoadRecords(
trans-> entry = 0012FD10, level = 0, maxlen = 256, discard = F, rawval = dbLoadRecords(
refer-> entry = 0012FD10, level = 0, maxlen = 241, rawval = $(AREA_
T=OVER1,
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = AREA_DETECTOR)
<-trans level = 1, length = 13, value = AREA_DETECTOR
lookup-> level = 0, name = AREA_DETECTOR, special = 0
<-lookup level = 0, name = AREA_DETECTOR, result = 010182A8
trans-> entry = 0012FD10, level = 1, maxlen = 241, discard = T, rawval = J:/epics/
<-trans level = 1, length = 31, value = J:/epics/
<-refer level = 0, length = 31, value = J:/epics/
refer-> entry = 0012FD10, level = 0, maxlen = 176, rawval = $(PREFIX)
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = PREFIX)
<-trans level = 1, length = 6, value = PREFIX
lookup-> level = 0, name = PREFIX, special = 0
<-lookup level = 0, name = PREFIX, result = 01038698
trans-> entry = 0012FD10, level = 1, maxlen = 176, discard = T, rawval = 13SIM1:
<-trans level = 1, length = 7, value = 13SIM1:
<-refer level = 0, length = 7, value = 13SIM1:
refer-> entry = 0012FD10, level = 0, maxlen = 122, rawval = $(PREFIX)
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = PREFIX)
<-trans level = 1, length = 6, value = PREFIX
lookup-> level = 0, name = PREFIX, special = 0
<-lookup level = 0, name = PREFIX, result = 01038698
trans-> entry = 0012FD10, level = 1, maxlen = 122, discard = T, rawval = 13SIM1:
<-trans level = 1, length = 7, value = 13SIM1:
<-refer level = 0, length = 7, value = 13SIM1:
refer-> entry = 0012FD10, level = 0, maxlen = 96, rawval = $(PREFIX)
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = PREFIX)
<-trans level = 1, length = 6, value = PREFIX
lookup-> level = 0, name = PREFIX, special = 0
<-lookup level = 0, name = PREFIX, result = 01038698
trans-> entry = 0012FD10, level = 1, maxlen = 96, discard = T, rawval = 13SIM1:
<-trans level = 1, length = 7, value = 13SIM1:
<-refer level = 0, length = 7, value = 13SIM1:
refer-> entry = 0012FD10, level = 0, maxlen = 69, rawval = $(PREFIX)
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = PREFIX)
<-trans level = 1, length = 6, value = PREFIX
lookup-> level = 0, name = PREFIX, special = 0
<-lookup level = 0, name = PREFIX, result = 01038698
trans-> entry = 0012FD10, level = 1, maxlen = 69, discard = T, rawval = 13SIM1:
<-trans level = 1, length = 7, value = 13SIM1:
<-refer level = 0, length = 7, value = 13SIM1:
refer-> entry = 0012FD10, level = 0, maxlen = 41, rawval = $(PREFIX)
trans-> entry = 0012FD10, level = 1, maxlen = 256, discard = T, rawval = PREFIX)
<-trans level = 1, length = 6, value = PREFIX
lookup-> level = 0, name = PREFIX, special = 0
<-lookup level = 0, name = PREFIX, result = 01038698
trans-> entry = 0012FD10, level = 1, maxlen = 41, discard = T, rawval = 13SIM1:
<-trans level = 1, length = 7, value = 13SIM1:
<-refer level = 0, length = 7, value = 13SIM1:
<-trans level = 0, length = 256, value = dbLoadRecords(
PORT=OVER1,ADDR=0,T
macExpandString() -> 256
I have not digested the output. To try to work around the problem, I modified the line:
n = macExpandString
to be
n = macExpandString
i.e. to tell macExpandString that the buffer is 1 character smaller than it really is.
Index: macLib/macEnv.c
=======
RCS file: /net/phoebus/
retrieving revision 1.2.2.6
diff -u -r1.2.2.6 macEnv.c
--- macLib/macEnv.c 23 Apr 2009 18:49:39 -0000 1.2.2.6
+++ macLib/macEnv.c 30 Mar 2010 14:23:26 -0000
@@ -38,7 +38,7 @@
*/
dest = mallocMustSucce
- n = macExpandString
+ n = macExpandString
} while (n >= (destCapacity - 1));
if (n < 0) {
That fixed the problem, but I think that probably the correct solution is to find where macExpandString is writing past the end of dest and fixing that.
Mark
Related branches
tags: | added: codeathon |
Changed in epics-base: | |
status: | In Progress → Fix Committed |
Changed in epics-base: | |
status: | Fix Committed → Fix Released |
With all this morning's correspondence in mind, here's a patch that I think fixes and clarifies things. The Application Developer's Guide should be changed to note the change in argument name (from maxlen to capacity). The AppDevGuide comment before macExpandString should be modified to add the note that the return value count does not include any trailing null.