Skip to content

Instantly share code, notes, and snippets.

@x42
Created February 10, 2019 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x42/b82769d2b3567b0a5c77c2bd857326ae to your computer and use it in GitHub Desktop.
Save x42/b82769d2b3567b0a5c77c2bd857326ae to your computer and use it in GitHub Desktop.
// gcc -g -o lv2forge lv2forge.c -Wall
#include <stdio.h>
#include <stdlib.h>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/forge.h>
#include <lv2/lv2plug.in/ns/ext/uri-map/uri-map.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#define ATOM_BUFSIZE (1024)
static char** urimap = NULL;
static uint32_t urimap_len = 0;
static uint32_t
uri_to_id (LV2_URI_Map_Callback_Data callback_data, const char* uri)
{
for (uint32_t i = 0; i < urimap_len; ++i) {
if (!strcmp (urimap[i], uri)) {
return i + 1;
}
}
urimap = (char**)realloc (urimap, (urimap_len + 1) * sizeof (char*));
urimap[urimap_len] = strdup (uri);
return ++urimap_len;
}
static LV2_URID_Map uri_map = { NULL, &uri_to_id };
int
main (int argc, char** argv)
{
/* host provided port */
LV2_Atom_Sequence* port = (LV2_Atom_Sequence*)malloc (ATOM_BUFSIZE + sizeof (uint8_t));
/* initialize sequence (host) */
port->atom.type = 0;
port->atom.size = ATOM_BUFSIZE;
/* "plugin" code starts here */
LV2_Atom_Forge forge;
LV2_URID_Map* map = &uri_map;
LV2_URID uri_1 = map->map (map->handle, "uri-1");
LV2_URID uri_2 = map->map (map->handle, "uri-2");
lv2_atom_forge_init (&forge, map);
/* re-use the same Atom port two times */
for (int i = 0; i < 2; ++i) {
LV2_Atom_Forge_Frame seq;
/* the 2nd time around "capacity" is the length of the data previously
* written to the port (!)
*/
const uint32_t capacity = port->atom.size;
printf ("Port Capacity %d\n", capacity);
lv2_atom_forge_set_buffer (&forge, (uint8_t*)port, capacity);
lv2_atom_forge_sequence_head (&forge, &seq, 0);
for (int j = 0; j < i; ++j) {
LV2_Atom_Forge_Frame frame;
lv2_atom_forge_frame_time (&forge, 0); //<< crash
lv2_atom_forge_object (&forge, &frame, 1, uri_1);
lv2_atom_forge_property_head (&forge, uri_2, 0);
lv2_atom_forge_float (&forge, 1.f);
lv2_atom_forge_pop (&forge, &frame);
}
/* close off sequence */
lv2_atom_forge_pop (&forge, &seq);
}
return 0;
}
@x42
Copy link
Author

x42 commented Feb 10, 2019

This shows that re-using a LV2 Atom port causes a crash (using 1.14.0 - debian 9.6)

@x42
Copy link
Author

x42 commented Feb 10, 2019

@drobilla
Copy link

I don't think that this is demonstrating a forge bug. You are giving an incorrect capacity to lv2_atom_forge_set_buffer(), the "plugin" code is broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment