Skip to content

Commit 445b415

Browse files
committed
fixup null terminating strings 2
1 parent 9bc256b commit 445b415

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

source/pdal/pdalc_pipeline.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ extern "C"
115115

116116
std::stringstream strm;
117117
pdal::PipelineWriter::writePipeline(ptr->manager->getStage(), strm);
118-
strm.get(buffer, size);
119-
return std::min(static_cast<size_t>(strm.gcount()), size);
118+
std::string out = strm.str();
119+
if (out.length() > size - 1) out.resize(size - 1);
120+
std::strncpy(buffer, out.c_str(), size);
121+
return std::min(out.length() + 1, size);
120122
}
121123
catch (const std::exception &e)
122124
{
@@ -141,8 +143,10 @@ extern "C"
141143
std::stringstream strm;
142144
MetadataNode root = ptr->manager->getMetadata().clone("metadata");
143145
pdal::Utils::toJSON(root, strm);
144-
strm.get(metadata, size);
145-
return std::min(static_cast<size_t>(strm.gcount()), size);
146+
std::string out = strm.str();
147+
if (out.length() > size - 1) out.resize(size - 1);
148+
std::strncpy(metadata, out.c_str(), size);
149+
return std::min(out.length() + 1, size);
146150
}
147151
catch (const std::exception &e)
148152
{
@@ -165,8 +169,10 @@ extern "C"
165169
MetadataNode meta = ptr->manager->pointTable().layout()->toMetadata();
166170
MetadataNode root = meta.clone("schema");
167171
pdal::Utils::toJSON(root, strm);
168-
strm.get(schema, size);
169-
return std::min(static_cast<size_t>(strm.gcount()), size);
172+
std::string out = strm.str();
173+
if (out.length() > size - 1) out.resize(size - 1);
174+
std::strncpy(schema, out.c_str(), size);
175+
return std::min(out.length() + 1, size);
170176
}
171177
catch (const std::exception &e)
172178
{

tests/pdal/test_pdalc_pipeline.c.in

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ TEST testPDALGetPipelineAsString(void)
146146
FAILm("PDALGetPipelineMetadata generated a JSON string whose object name is not \"pipeline\"");
147147
}
148148

149+
char json10[10];
150+
size = PDALGetPipelineAsString(pipeline, json10, 10);
151+
152+
if (size != 10 )
153+
{
154+
PDALDisposePipeline(pipeline);
155+
pipeline = NULL;
156+
FAILm("PDALGetPipelineAsString truncation not working");
157+
}
158+
159+
if (json[0] == '\0')
160+
{
161+
PDALDisposePipeline(pipeline);
162+
pipeline = NULL;
163+
FAILm("PDALGetPipelineAsString generated a JSON string whose first character is null");
164+
}
165+
149166
PDALDisposePipeline(pipeline);
150167
PASS();
151168
}
@@ -200,6 +217,16 @@ TEST testPDALGetPipelineMetadata(void)
200217
FAILm("PDALGetPipelineMetadata generated a JSON string whose object name is not \"schema\"");
201218
}
202219

220+
char json10[10];
221+
size = PDALGetPipelineMetadata(pipeline, json10, 10);
222+
223+
if (size != 10 )
224+
{
225+
PDALDisposePipeline(pipeline);
226+
pipeline = NULL;
227+
FAILm("PDALGetPipelineMetadata truncation not working");
228+
}
229+
203230
PDALDisposePipeline(pipeline);
204231
PASS();
205232
}
@@ -254,6 +281,16 @@ TEST testPDALGetPipelineSchema(void)
254281
FAILm("PDALGetPipelineSchema generated a JSON string whose object name is not \"schema\"");
255282
}
256283

284+
char json10[10];
285+
size = PDALGetPipelineSchema(pipeline, json10, 10);
286+
287+
if (size != 10 )
288+
{
289+
PDALDisposePipeline(pipeline);
290+
pipeline = NULL;
291+
FAILm("PDALGetPipelineSchema truncation not working");
292+
}
293+
257294
PDALDisposePipeline(pipeline);
258295
PASS();
259296
}

0 commit comments

Comments
 (0)