os/vfs.h: handle zero-sized files

The 'File_content' class failed to consider zero-sized files.

Fixes #3557
This commit is contained in:
Norman Feske 2019-11-29 11:38:19 +01:00 committed by Christian Helmuth
parent 2aa6471608
commit f85ec313de
1 changed files with 19 additions and 3 deletions

View File

@ -498,8 +498,15 @@ class Genode::File_content
template <typename FN>
void xml(FN const &fn) const
{
try { fn(Xml_node(_buffer.ptr, _buffer.size)); }
catch (Xml_node::Invalid_syntax) { fn(Xml_node("<empty/>")); }
try {
if (_buffer.size) {
fn(Xml_node(_buffer.ptr, _buffer.size));
return;
}
}
catch (Xml_node::Invalid_syntax) { }
fn(Xml_node("<empty/>"));
}
/**
@ -510,6 +517,9 @@ class Genode::File_content
template <typename STRING, typename FN>
void for_each_line(FN const &fn) const
{
if (_buffer.size == 0)
return;
char const *src = _buffer.ptr;
char const *curr_line = src;
size_t curr_line_len = 0;
@ -538,9 +548,15 @@ class Genode::File_content
/**
* Call functor 'fn' with the data pointer and size in bytes
*
* If the buffer has a size of zero, 'fn' is not called.
*/
template <typename FN>
void bytes(FN const &fn) const { fn((char const *)_buffer.ptr, _buffer.size); }
void bytes(FN const &fn) const
{
if (_buffer.size)
fn((char const *)_buffer.ptr, _buffer.size);
}
};