* bus/activation.c, bus/desktop-file.c: Distinguish between OOM and
[platform/upstream/dbus.git] / bus / desktop-file.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* desktop-file.c  .desktop file parser
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include <dbus/dbus-sysdeps.h>
25 #include <dbus/dbus-internals.h>
26 #include "desktop-file.h"
27 #include "utils.h"
28
29 typedef struct
30 {
31   char *key;
32   char *value;
33 } BusDesktopFileLine;
34
35 typedef struct
36 {
37   char *section_name;
38   
39   int n_lines;
40   BusDesktopFileLine *lines;
41   int n_allocated_lines;  
42 } BusDesktopFileSection;
43
44 struct BusDesktopFile
45 {
46   int n_sections;
47   BusDesktopFileSection *sections;
48   int n_allocated_sections;
49 };
50
51 /**
52  * Parser for service files.
53  */
54 typedef struct
55 {
56   DBusString data; /**< The data from the file */
57
58   BusDesktopFile *desktop_file; /**< The resulting object */
59   int current_section;    /**< The current section being parsed */
60   
61   int pos;          /**< Current position */
62   int len;          /**< Length */
63   int line_num;     /**< Current line number */
64   
65 } BusDesktopFileParser;
66
67 #define VALID_KEY_CHAR 1
68 #define VALID_LOCALE_CHAR 2
69 unsigned char valid[256] = { 
70    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
71    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
72    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 , 
73    0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
74    0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 
75    0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x2 , 
76    0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 
77    0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
78    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
79    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
80    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
81    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
82    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
83    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
84    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
85    0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 
86 };
87
88 static void report_error (BusDesktopFileParser *parser,
89                           char                 *message,
90                           const char           *error_name,
91                           DBusError            *error);
92
93 static void
94 parser_free (BusDesktopFileParser *parser)
95 {
96   bus_desktop_file_free (parser->desktop_file);
97   
98   _dbus_string_free (&parser->data);
99 }
100
101 static void
102 bus_desktop_file_line_free (BusDesktopFileLine *line)
103 {
104   dbus_free (line->key);
105   dbus_free (line->value);
106 }
107
108 static void
109 bus_desktop_file_section_free (BusDesktopFileSection *section)
110 {
111   int i;
112
113   for (i = 0; i < section->n_lines; i++)
114     bus_desktop_file_line_free (&section->lines[i]);
115
116   dbus_free (section->lines);
117   dbus_free (section->section_name);
118 }
119
120 void
121 bus_desktop_file_free (BusDesktopFile *desktop_file)
122 {
123   int i;
124
125   for (i = 0; i < desktop_file->n_sections; i++)
126     bus_desktop_file_section_free (&desktop_file->sections[i]);
127   dbus_free (desktop_file->sections);
128
129   dbus_free (desktop_file);
130 }
131
132 static dbus_bool_t
133 grow_lines_in_section (BusDesktopFileSection *section)
134 {
135   BusDesktopFileLine *lines;
136   
137   int new_n_lines;
138
139   if (section->n_allocated_lines == 0)
140     new_n_lines = 1;
141   else
142     new_n_lines = section->n_allocated_lines*2;
143
144   lines = dbus_realloc (section->lines,
145                         sizeof (BusDesktopFileLine) * new_n_lines);
146
147   if (lines == NULL)
148     return FALSE;
149   
150   section->lines = lines;
151   section->n_allocated_lines = new_n_lines;
152
153   return TRUE;
154 }
155
156 static dbus_bool_t
157 grow_sections (BusDesktopFile *desktop_file)
158 {
159   int new_n_sections;
160   BusDesktopFileSection *sections;
161   
162   if (desktop_file->n_allocated_sections == 0)
163     new_n_sections = 1;
164   else
165     new_n_sections = desktop_file->n_allocated_sections*2;
166
167   sections = dbus_realloc (desktop_file->sections,
168                            sizeof (BusDesktopFileSection) * new_n_sections);
169   if (sections == NULL)
170     return FALSE;
171   
172   desktop_file->sections = sections;
173   
174   desktop_file->n_allocated_sections = new_n_sections;
175
176   return TRUE;
177 }
178
179 static char *
180 unescape_string (BusDesktopFileParser *parser,
181                  const DBusString     *str,
182                  int                   pos,
183                  int                   end_pos,
184                  DBusError            *error)
185 {
186   char *retval, *q;
187
188   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
189   
190   /* len + 1 is enough, because unescaping never makes the
191    * string longer
192    */
193   retval = dbus_malloc (end_pos - pos + 1);
194   if (retval == NULL)
195     {
196       BUS_SET_OOM (error);
197       return NULL;
198     }
199
200   q = retval;
201   
202   while (pos < end_pos)
203     {
204       if (_dbus_string_get_byte (str, pos) == 0)
205         {
206           /* Found an embedded null */
207           dbus_free (retval);
208           report_error (parser, "Text to be unescaped contains embedded nul",
209                         BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
210           return NULL;
211         }
212
213       if (_dbus_string_get_byte (str, pos) == '\\')
214         {
215           pos ++;
216
217           if (pos >= end_pos)
218             {
219               /* Escape at end of string */
220               dbus_free (retval);
221               report_error (parser, "Text to be unescaped ended in \\",
222                             BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
223               return NULL;
224             }
225
226           switch (_dbus_string_get_byte (str, pos))
227             {
228             case 's':
229               *q++ = ' ';
230               break;
231            case 't':
232               *q++ = '\t';
233               break;
234            case 'n':
235               *q++ = '\n';
236               break;
237            case 'r':
238               *q++ = '\r';
239               break;
240            case '\\':
241               *q++ = '\\';
242               break;
243            default:
244              /* Invalid escape code */
245              dbus_free (retval);
246              report_error (parser, "Text to be unescaped had invalid escape sequence",
247                            BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error);
248              return NULL;
249             }
250           pos++;
251         }
252       else
253         {
254           *q++ =_dbus_string_get_byte (str, pos);
255
256           pos++;
257         }
258     }
259
260   *q = 0;
261
262   return retval;
263 }
264
265 static BusDesktopFileSection* 
266 new_section (BusDesktopFile *desktop_file,
267              const char     *name)
268 {
269   int n;
270   char *name_copy;
271   
272   if (desktop_file->n_allocated_sections == desktop_file->n_sections)
273     {
274       if (!grow_sections (desktop_file))
275         return NULL;
276     }
277
278   name_copy = _dbus_strdup (name);
279   if (name_copy == NULL)
280     return NULL;
281
282   n = desktop_file->n_sections;
283   desktop_file->sections[n].section_name = name_copy;
284
285   desktop_file->sections[n].n_lines = 0;
286   desktop_file->sections[n].lines = NULL;
287   desktop_file->sections[n].n_allocated_lines = 0;
288
289   if (!grow_lines_in_section (&desktop_file->sections[n]))
290     {
291       dbus_free (desktop_file->sections[n].section_name);
292       desktop_file->sections[n].section_name = NULL;
293       return NULL;
294     }
295
296   desktop_file->n_sections += 1;
297   
298   return &desktop_file->sections[n];  
299 }
300
301 static BusDesktopFileSection* 
302 open_section (BusDesktopFileParser *parser,
303               char                 *name)
304 {  
305   BusDesktopFileSection *section;
306
307   section = new_section (parser->desktop_file, name);
308   if (section == NULL)
309     return NULL;
310   
311   parser->current_section = parser->desktop_file->n_sections - 1;
312   _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section);
313   
314   return section;
315 }
316
317 static BusDesktopFileLine *
318 new_line (BusDesktopFileParser *parser)
319 {
320   BusDesktopFileSection *section;
321   BusDesktopFileLine *line;
322   
323   section = &parser->desktop_file->sections[parser->current_section];
324
325   if (section->n_allocated_lines == section->n_lines)
326     {
327       if (!grow_lines_in_section (section))
328         return NULL;
329     }
330
331   line = &section->lines[section->n_lines++];
332
333   memset (line, 0, sizeof (BusDesktopFileLine));
334     
335   return line;
336 }
337
338 static dbus_bool_t
339 is_blank_line (BusDesktopFileParser *parser)
340 {
341   int p;
342   char c;
343   
344   p = parser->pos;
345
346   c = _dbus_string_get_byte (&parser->data, p);
347
348   while (c && c != '\n')
349     {
350       if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'))
351         return FALSE;
352       
353       p++;
354       c = _dbus_string_get_byte (&parser->data, p);
355     }
356
357   return TRUE;
358 }
359
360 static void
361 parse_comment_or_blank (BusDesktopFileParser *parser)
362 {
363   int line_end;
364   
365   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
366     line_end = parser->len;
367
368   if (line_end == parser->len)
369     parser->pos = parser->len;
370   else
371     parser->pos = line_end + 1;
372   
373   parser->line_num += 1;
374 }
375
376 static dbus_bool_t
377 is_valid_section_name (const char *name)
378 {
379   /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */
380
381   while (*name)
382     {
383       if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') ||
384             *name == '\n' || *name == '\t'))
385         return FALSE;
386       
387       name++;
388     }
389
390   return TRUE;
391 }
392
393 static dbus_bool_t
394 parse_section_start (BusDesktopFileParser *parser, DBusError *error)
395 {
396   int line_end;
397   char *section_name;
398
399   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
400   
401   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
402     line_end = parser->len;
403   
404   if (line_end - parser->pos <= 2 ||
405       _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
406     {
407       report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
408       parser_free (parser);
409       return FALSE;
410     }
411
412   section_name = unescape_string (parser,
413                                   &parser->data, parser->pos + 1, line_end - 1,
414                                   error);
415
416   if (section_name == NULL)
417     {
418       parser_free (parser);
419       return FALSE;
420     }
421
422   if (!is_valid_section_name (section_name))
423     {
424       report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
425       parser_free (parser);
426       dbus_free (section_name);
427       return FALSE;
428     }
429
430   if (open_section (parser, section_name) == NULL)
431     {
432       dbus_free (section_name);
433       return FALSE;
434     }
435
436   if (line_end == parser->len)
437     parser->pos = parser->len;
438   else
439     parser->pos = line_end + 1;
440   
441   parser->line_num += 1;
442
443   dbus_free (section_name);
444   
445   return TRUE;
446 }
447
448 static dbus_bool_t
449 parse_key_value (BusDesktopFileParser *parser, DBusError *error)
450 {
451   int line_end;
452   int key_start, key_end;
453   int value_start;
454   int p;
455   char *value, *tmp;
456   DBusString key;
457   BusDesktopFileLine *line;
458
459   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
460   
461   if (!_dbus_string_find (&parser->data, parser->pos, "\n", &line_end))
462     line_end = parser->len;
463   
464   p = parser->pos;
465   key_start = p;
466   while (p < line_end &&
467          (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR))
468     p++;
469   key_end = p;
470   
471   if (key_start == key_end)
472     {
473       report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
474       parser_free (parser);
475       return FALSE;
476     }
477
478   /* We ignore locales for now */
479   if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[')
480     {
481       if (line_end == parser->len)
482         parser->pos = parser->len;
483       else
484         parser->pos = line_end + 1;
485           
486       parser->line_num += 1;
487
488       return TRUE;
489     }
490   
491   /* Skip space before '=' */
492   while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
493     p++;
494
495   if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=')
496     {
497       report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
498       parser_free (parser);
499       return FALSE;
500     }
501
502   if (p == line_end)
503     {
504       report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
505       parser_free (parser);
506       return FALSE;
507     }
508
509   /* Skip the '=' */
510   p++;
511
512   /* Skip space after '=' */
513   while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ')
514     p++;
515
516   value_start = p;
517   
518   value = unescape_string (parser, &parser->data, value_start, line_end, error);
519   if (value == NULL)
520     {
521       parser_free (parser);
522       return FALSE;
523     }
524
525   line = new_line (parser);
526   if (line == NULL)
527     {
528       dbus_free (value);
529       parser_free (parser);
530       return FALSE;
531     }
532   
533   if (!_dbus_string_init (&key))
534     {
535       dbus_free (value);
536       parser_free (parser);
537       return FALSE;
538     }
539   
540   if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start,
541                               &key, 0))
542     {
543       dbus_free (value);
544       parser_free (parser);
545       return FALSE;
546     }
547   
548   if (!_dbus_string_steal_data (&key, &tmp))
549     {
550       dbus_free (value);
551       parser_free (parser);
552       return FALSE;
553     }
554   
555   _dbus_string_free (&key);
556   
557   line->key = tmp;
558   line->value = value;
559
560   if (line_end == parser->len)
561     parser->pos = parser->len;
562   else
563     parser->pos = line_end + 1;
564   
565   parser->line_num += 1;
566
567   return TRUE;
568 }
569
570 static void
571 report_error (BusDesktopFileParser *parser,
572               char                 *message,
573               const char           *error_name,
574               DBusError            *error)
575 {
576   const char *section_name = NULL;
577
578   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
579   
580   if (parser->current_section != -1)
581     section_name = parser->desktop_file->sections[parser->current_section].section_name;
582
583   if (section_name)
584     dbus_set_error (error, error_name,
585                     "Error in section %s at line %d: %s\n", section_name, parser->line_num, message);
586   else
587     dbus_set_error (error, error_name,
588                     "Error at line %d: %s\n", parser->line_num, message);
589 }
590
591 #if 0
592 static void
593 dump_desktop_file (BusDesktopFile *file)
594 {
595   int i;
596
597   for (i = 0; i < file->n_sections; i++)
598     {
599       int j;
600       
601       printf ("[%s]\n", file->sections[i].section_name);
602
603       for (j = 0; j < file->sections[i].n_lines; j++)
604         {
605           printf ("%s=%s\n", file->sections[i].lines[j].key,
606                   file->sections[i].lines[j].value);
607         }
608     }
609 }
610 #endif
611
612 BusDesktopFile*
613 bus_desktop_file_load (DBusString *filename,
614                        DBusError  *error)
615 {
616   DBusString str;
617   BusDesktopFileParser parser;
618   DBusStat sb;
619
620   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
621   
622   /* Clearly there's a race here, but it's just to make it unlikely
623    * that we do something silly, we still handle doing it below.
624    */
625   if (!_dbus_stat (filename, &sb, error))
626     return NULL;
627
628   if (sb.size > _DBUS_ONE_KILOBYTE * 128)
629     {
630       dbus_set_error (error, DBUS_ERROR_FAILED,
631                       "Desktop file size (%ld bytes) is too large", (long) sb.size);
632       return NULL;
633     }
634   
635   if (!_dbus_string_init (&str))
636     return NULL;
637   
638   if (!_dbus_file_get_contents (&str, filename, error))
639     {
640       _dbus_string_free (&str);
641       return NULL;
642     }
643
644   if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
645     {
646       _dbus_string_free (&str);
647       dbus_set_error (error, DBUS_ERROR_FAILED,
648                       "invalid UTF-8");   
649       return NULL;
650     }
651   
652   parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
653   if (parser.desktop_file == NULL)
654     {
655       _dbus_string_free (&str);
656       BUS_SET_OOM (error);
657       return NULL;
658     }
659   
660   parser.data = str;
661   parser.line_num = 1;
662   parser.pos = 0;
663   parser.len = _dbus_string_get_length (&parser.data);
664   parser.current_section = -1;
665
666   while (parser.pos < parser.len)
667     {
668       if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
669         {
670           if (!parse_section_start (&parser, error))
671             {
672               return NULL;
673             }
674         }
675       else if (is_blank_line (&parser) ||
676                _dbus_string_get_byte (&parser.data, parser.pos) == '#')
677         parse_comment_or_blank (&parser);
678       else
679         {
680           if (!parse_key_value (&parser, error))
681             {
682               return NULL;
683             }
684         }
685     }
686
687   _dbus_string_free (&parser.data);
688
689   return parser.desktop_file;
690 }
691
692 static BusDesktopFileSection *
693 lookup_section (BusDesktopFile *desktop_file,
694                 const char     *section_name)
695 {
696   BusDesktopFileSection *section;
697   int i;
698   
699   if (section_name == NULL)
700     return NULL;
701   
702   for (i = 0; i < desktop_file->n_sections; i ++)
703     {
704       section = &desktop_file->sections[i];
705
706       if (strcmp (section->section_name, section_name) == 0)
707         return section;
708     }
709   
710   return NULL;
711 }
712
713 static BusDesktopFileLine *
714 lookup_line (BusDesktopFile        *desktop_file,
715              BusDesktopFileSection *section,
716              const char            *keyname)
717 {
718   BusDesktopFileLine *line;
719   int i;
720
721   for (i = 0; i < section->n_lines; i++)
722     {
723       line = &section->lines[i];
724       
725       if (strcmp (line->key, keyname) == 0)
726         return line;
727     }
728   
729   return NULL;
730 }
731
732 dbus_bool_t
733 bus_desktop_file_get_raw (BusDesktopFile  *desktop_file,
734                           const char      *section_name,
735                           const char      *keyname,
736                           const char     **val)
737 {
738   BusDesktopFileSection *section;
739   BusDesktopFileLine *line;
740
741   *val = NULL;
742
743   section = lookup_section (desktop_file, section_name);
744   
745   if (!section)
746     return FALSE;
747
748   line = lookup_line (desktop_file,
749                       section,
750                       keyname);
751
752   if (!line)
753     return FALSE;
754   
755   *val = line->value;
756   
757   return TRUE;
758 }
759
760 dbus_bool_t
761 bus_desktop_file_get_string (BusDesktopFile  *desktop_file,
762                              const char      *section,
763                              const char      *keyname,
764                              char           **val,
765                              DBusError       *error)
766 {
767   const char *raw;
768  
769   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
770
771   *val = NULL;
772   
773   if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw))
774     {
775       dbus_set_error (error, DBUS_ERROR_FAILED,
776                       "No \"%s\" key in .service file\n", keyname);
777       return FALSE;
778     }
779
780   *val = _dbus_strdup (raw);
781
782   if (*val == NULL)
783     {
784       BUS_SET_OOM (error);
785       return FALSE;
786     }
787   
788   return TRUE;
789 }