4coder push_stringf bug?

Salutations,

I've been trying to use the file path of the active buffer as a parameter for my build.bat. This means that whenever I hit my FKey command for building, it should also include the filepath of the buffer I was inside when I hit the F-key. This is useful for practising when I just want to compile one executable for one c/c++-file.

From 4coder_project_commands.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function void
prj_exec_command(Application_Links *app, Variable_Handle cmd_var){
    Scratch_Block scratch(app);
    
    String_ID os_id = vars_save_string_lit(OS_NAME);
    
    String8 cmd = vars_string_from_var(scratch, vars_read_key(cmd_var, os_id));
    if (cmd.size > 0){

		// NOTE(Bobby): Uses the file path of the active buffer as a parameter
		
		View_ID ActiveView = get_active_view(app, Access_Visible);
		Buffer_ID ActiveBuffer = view_get_buffer(app, ActiveView, Access_Visible);
		
		// NOTE(Bobby): Is this a bug?
		// NOTE(Bobby): CmdWithBufferParam sometimes contains characters from BufferName
		// or just junk data, why?

		String_Const_u8 BufferName = push_buffer_file_name(app, scratch, ActiveBuffer);
		String_Const_u8 CmdWithBufferParam = push_stringf(scratch, "%s %s", cmd.str, BufferName.str);

		
		String_Const_u8 ch = SCu8("\n---------------------------\n");
		
		log_string(app, ch);
		log_string(app, CmdWithBufferParam);
		log_string(app, ch);
                ...
		cmd = CmdWithBufferParam;
   }
...
}


Notice how the 'BufferName' string is always correct but 'cmd' string contains data from 'BufferName'.
1
2
3
4
*log*:
---------------------------
build.batcoder_bC:\Program Files\4coder\4coder_bobby\4coder_bobby.cpp C:\Program Files\4coder\4coder_bobby\4coder_bobby.cpp
---------------------------

This happens for other c/cpp files as well. For instance, doing this from any cpp file from the custom folder in 4coder will also yield the same bug.

This is the correct output:
1
2
3
4
*log*:
---------------------------
build.bat C:\Program Files\4coder\4coder_bobby\4coder_bobby.cpp
---------------------------


Sometimes it works, sometimes it doesn't. But I'm wondering, am I doing something wrong?

Edited by Bobby on
I think the issue is that you use non zero terminated strings in push_stringf.
vars_string_from_var (and maybe push_buffer_file_name) doesn't add the null terminator, so when you use cmd.str in push_stringf, it reads all the bytes until a null terminator. I didn't check the code to confirm this.

There might be a format specifier to use String8 and String_Const_u8 in push_stringf, I don't know. You can look at 4coder_base_types.cpp to find more string functions.

1
2
3
4
5
6
7
8
9
 
String8 cmd = vars_string_from_var(scratch, vars_read_key(cmd_var, os_id));
// scratch contains "build.bat"
String_Const_u8 BufferName = push_buffer_file_name(app, scratch, ActiveBuffer);
// scratch contains "build.bat???????C:\Program Files\4coder\4coder_bobby\4coder_bobby.cpp"
// ? is garbage, I believe pushing something on a arena is always 8 bytes aligned in 4coder.
String_Const_u8 CmdWithBufferParam = push_stringf(scratch, "%s %s", cmd.str, BufferName.str);
// The first %s will read until there is a zero bytes, which in your case seems to be after the file path.
// Maybe push_buffer_file_name always adds a null terminator, I don't know.
You can printf 4Coder strings like this:
1
String_Const_u8 cmd = push_stringf(scratch, "%.*s", string_expand(string));


"%.*s" expects a string length and the string pointer, which is exactly what the string_expand macro does.
marcc
You can printf 4Coder strings like this:
1
String_Const_u8 cmd = push_stringf(scratch, "%.*s", string_expand(string));


"%.*s" expects a string length and the string pointer, which is exactly what the string_expand macro does.


This worked.

Thank you, both. I'm still a little bit overwhelmed by the amount of ways 4coder construct/handles strings.

Edited by Bobby on