Skip to content

Commit b91fda7

Browse files
Fixed error caused by buffer overflow on Linux
1 parent 59dfca2 commit b91fda7

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

FEBioLib/Logfile.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ void Logfile::printbox(const char* sztitle, const char* sz, ...)
138138
char right[60] = {0};
139139
strncpy(left, sztitle, l/2);
140140
strncpy(right, sztitle+l/2, l - l/2);
141-
snprintf(ch,1024, " * %33s", left); ch += strlen(ch);
142-
snprintf(ch,1024, "%-36s *\n", right); ch += strlen(ch);
141+
142+
size_t remaining = sizeof(szmsg) - (ch - szmsg);
143+
snprintf(ch,remaining, " * %33s", left); ch += strlen(ch);
144+
remaining = sizeof(szmsg) - (ch - szmsg);
145+
snprintf(ch,remaining, "%-36s *\n", right); ch += strlen(ch);
143146
// snprintf(ch,1024, " *%71s*\n", ""); ch += strlen(ch);
144147
}
145148

@@ -162,13 +165,15 @@ void Logfile::printbox(const char* sztitle, const char* sz, ...)
162165
if (cn) *cn = '\n';
163166
cn = ct + n;
164167
}
165-
snprintf(ch,1024," * %-69s *\n", ct); ch += strlen(ch);
168+
size_t remaining = sizeof(szmsg) - (ch - szmsg);
169+
snprintf(ch,remaining," * %-69s *\n", ct); ch += strlen(ch);
166170
if (wrap) { ct[n] = tmp; }
167171
if (cn) ct = cn+1;
168172
}
169173
while (cn);
170174
// sprintf(ch," * *\n"); ch += strlen(ch);
171-
snprintf(ch,1024," *************************************************************************\n");
175+
size_t remaining = sizeof(szmsg) - (ch - szmsg);
176+
snprintf(ch,remaining," *************************************************************************\n");
172177

173178
// print the message
174179
printf(szmsg);

0 commit comments

Comments
 (0)