New answers tagged

2

Undefined behavior strcmp(left, token), strdup(right) are UB as left, right do not certainly point to strings (null character terminated arrays). Could replace strncpy(left, fbuf, dlim_ptr - fbuf); with below to insure null termination. sprintf(left, "%.*s", (int) (dlim_ptr - fbuf), fbuf);


1

Ok, first some comments. I’ll add code if you want - just can’t do it effectively from my phone. Basically any string function that doesn’t take the length of the string is problematic. It has the potential to read beyond the end of the buffer. atoi should be replaced with strtol, strcmp with strncmp, ... Allocating fixed sizes of memory, just to ...


-2

Well I dont know exactly what strstr does but i would've used strtok (string tokenizer) somewhere in the process as its purpose is to find delimiters and split strings there.


6

__func__ Unlike __FUNCTION__, __func__ is standard C since C99. Some compilers might not support C99 and therefore in those compilers you have to use whatever they provide, but if your compiler supports C99, you should use __func__. If you have a crappy compiler, think about using a better one. magic numbers What is a magic number, and why is it bad? ...


5

Your program crashes when it reads a line from the parameters file that doesn't contain an = sign. The name of the macro TEXT is misleading. It doesn't contain a text but a filename. Whenever you output an error message, it belongs on stderr instead of stdout. To do this, replace printf( with fprintf(stderr,.


7

Interface I'm not overly fond of the interface you've defined to the function. I think trying to combine reading integers and reading strings into a single function makes it more difficult to use. For most C code, I think the old guiding principle of UNIX ("do one thing, and do it well") provides excellent guidance. As such, I'd probably have two separate ...


2

Simplifications for the pattern usage you currently have: The pattern in parseMessage can be extended so you don't need to explicitly use dropWords. That only works if you change the signature to something based on words instead of the whole line, though. That is the first thing you may want to adjust. As it now stands you're doing a lot of work with "...


3

Note: I realize this was asked in 2014, and many python style requirements may have been different. Docstrings: You should include a docstring at the beginning of every method, class, and module you write. This will help any documentation identify what your program is supposed to do. Imports: You imported path from os in your method file_does_exist. This ...


3

In addition: "Always specify a radix when using parseInt." -- MDN parseInt documentation When getValue() is given a malformed string, it ignores the malformed parts without letting anyone know something went wrong. For example, 2 h 500 m 600 s returns 0. It is better to throw an error when the input isn't valid (so the caller can more easily figure out why ...


10

Named Capture Groups JavaScript RegExp has named capture groups that can make life a lot simpler when dealing with complicated RegExp. Combined with destructuring assignment you can extract the named hours minutes and seconds as follows. function toSeconds(time) { const {groups: {h = 0, m = 0, s = 0}} = /(?<h>\d*)h(?<m>\d*)m(?<s>\d*)/...


14

getValue(str) is such a vague name for the function and its parameter, it could mean anything! Furthermore, "get" implies that this is a getter function that retrieves something, which is not the case. Your regex is ineffective. Capturing parentheses could be useful, but you didn't actually use them right, such that you ended up having to pass a dirty ...


5

public static string RemoveStackStrace(string exceptionString) { // Stack-trace begins at the first 'at' return Regex.Split(exceptionString, @"^\s{3}at", RegexOptions.Multiline).First(); } I assume the method name is a typo for RemoveStackTrace. Regex seems overkill, as does \s{3} instead of simply " at". Is there any circumstance ...


1

fn str_to_vec3(s: &str) -> Option<Vec3> { let bits: Vec<&str> = s.split(" ").collect(); if bits.len() != 3 { return None; } Some(Vec3 { x: bits[0].parse().unwrap(), y: bits[1].parse().unwrap(), z: bits[2].parse().unwrap(), }) } For some errors, you panic (if parsing fails) and for ...


Top 50 recent answers are included